How to make UIToolbar have a Clear Background?

送分小仙女□ 提交于 2019-12-03 11:33:40

问题


I have a UIToolbar that has a white tint, with a bar button item, followed by some flexible space, followed by another bar button item. I would like to make the toolbar completely clear so that I can see what is under the flexible space (I don't care about seeing what is behind the buttons). Is there a way to do this? I have tried setting the toolbar to translucent, but that does not make it completely clear.


回答1:


[self.toolbar setBackgroundImage:[UIImage new]
              forToolbarPosition:UIToolbarPositionAny
                      barMetrics:UIBarMetricsDefault];

 [self.toolbar setBackgroundColor:[UIColor clearColor]];



回答2:


Subclass UIToolbar, and implement the below method:

- (void)drawRect:(CGRect)rect 
{
  [[UIColor colorWithWhite:0 alpha:0.6f] set]; // or clearColor etc
  CGContextFillRect(UIGraphicsGetCurrentContext(), rect);
}

see more details here




回答3:


set toolbarStyle -1 like this

 tools.barStyle = -1; // clear background



回答4:


Hacky, sorry, but the only way I've found so far that reliably works in both iOS 7 and iOS 6 is to do this:

[[toolbar.subviews objectAtIndex:0] removeFromSuperview];



回答5:


If you want a global solution take advantage of the UIAppearance proxy:

UIToolbar *toolbarAppearance = [UIToolbar appearance]; [toolbarAppearance setBackgroundImage:[[UIImage alloc] init] forToolbarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];




回答6:


It can be done without subclassing in iOS 6+ with setting the property translucent to `YES.

This will not work in iOS 5 in 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];



回答7:


Swift 3 version of accepted answer:

   self.toolbar.isTranslucent = true
    self.toolbar.setBackgroundImage(UIImage(),
                               forToolbarPosition: UIBarPosition.any,
                               barMetrics: UIBarMetrics.default)

    self.toolbar.setShadowImage(UIImage(), forToolbarPosition: UIBarPosition.any)


来源:https://stackoverflow.com/questions/12058208/how-to-make-uitoolbar-have-a-clear-background

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!