Transparent UIToolbar

前端 未结 7 955
鱼传尺愫
鱼传尺愫 2021-02-14 19:29

I wrote the following code to make my toolbar transparent.

[mtoolbar setBackgroundColor:[UIColor clearColor]];

How do I make UIToolbar

相关标签:
7条回答
  • 2021-02-14 19:46

    This worked for me for iOS 6 and 7:

    UIGraphicsBeginImageContextWithOptions(CGSizeMake(1, 1), NO, 0.0);
    UIImage *blank = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    [self.toolBar setBackgroundImage:blank forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
    
    0 讨论(0)
  • 2021-02-14 19:53
    for (UIView * sv in [toolBar subviews])
    {
         [sv removeFromSuperview];
    }
    

    ;) any iOs

    0 讨论(0)
  • 2021-02-14 19:56

    The following works in iOS 5 (and iOS 6 beta 4, although a slight top shadow is still visible there).

    Please note: Making a UIToolbar or UINavigationBar transparent is rarely a good idea, and modifying Apple's UIKit elements in such a way is bound to break sooner or later.

    TransparentToolbar.h

    #import <UIKit/UIKit.h>
    
    @interface TransparentToolbar : UIToolbar
    
    @end
    

    TransparentToolbar.m

    #import "TransparentToolbar.h"
    
    @implementation TransparentToolbar
    
    -(void)insertSubview:(UIView *)view atIndex:(NSInteger)index
    {
        //  This method is called with a view of class "UINavigationBarBackground" or "_UIToolbarBackground", respectively. It would be possible to check for this with NSStringFromClass([view class]) to be completely sure that we're skipping the right view.
    
        if (index != 0)
        {
            [super insertSubview:view atIndex:index];
        }
        else
        {
            // insert your custom background view, if you want to
        }
    }
    
    
    @end
    

    EDIT: In iOS 5+, it's also possible to simply set the backgroundImage (which could be transparent). This is certainly the "cleaner" solution, but is less flexible than a custom UIView.

    [someToolbar setBackgroundImage:[UIImage imageNamed:@"clear"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
    
    0 讨论(0)
  • 2021-02-14 20:03

    Check the below code

    [myToolbar setBarStyle:UIBarStyleBlack];
    [myToolbar setTranslucent:YES];
    

    Taken from

    @Brandon Bodnár has answered in the below SO post.

    Couldn't UIToolBar be transparent?

    you could also use the different approach

    Transparent UIToolBar

    0 讨论(0)
  • 2021-02-14 20:05

    You can set the property translucent to YES and see if this helps.

    0 讨论(0)
  • 2021-02-14 20:06

    Setting the property translucent to YES will not work in iOS 5 and below. Here's how it can be done without subclassing toolbar:

    const float colorMask[6] = {222, 255, 222, 255, 222, 255};
    UIImage *img = [[UIImage alloc] init];
    UIImage *maskedImage = [UIImage imageWithCGImage: CGImageCreateWithMaskingColors(img.CGImage, colorMask)];
    
    [self.toolbar setBackgroundImage:maskedImage forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
    
    0 讨论(0)
提交回复
热议问题