UIToolBar with reduced alpha, want UIBarButtonItem to have alpha-1

I have a UIToolbar with alpha setting to .6. It looks great - but it also leaves buttons with alpha .6, which is good, but not very desirable. Is there any way to set the UIBarButtonItem alpha myself? How can I make them look white rather than slightly gray?

+6
ios uitoolbar uibarbuttonitem
source share
6 answers

When you set the alpha for viewing, all sub-items are linked with the same alpha value. What you can do instead of setting alpha on the view itself is the hue color or the background color with the alpha value. This can lead to similar effects without causing all subclauses to inherit transparency.

toolbar.tintColor = [[UIColor blackColor] colorWithAlphaComponent:0.6]; 

If this does not work, and you really need to make the view transparent, you will have to redo your view hierarchy so that the opaque views are not nested. Since control panel items are not available in standard views, this probably will not work in the toolbar body.

+12
source share
 self.bottomToolbar.barStyle = UIBarStyleBlack; self.bottomToolbar.translucent = YES; self.bottomToolbar.backgroundColor = [UIColor clearColor]; 

View the opacity set to 1 and these settings did it for me.

+12
source share

Above did not work for me in iOS 4.3, but it was done:

Subclass of UIToolbar, specify one of the following methods:

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

I found that barStyle really matters. Also, the background color is not set.

 self.toolbar.barStyle = UIBarStyleDefault; self.toolbar.tintColor = [UIColor colorWithWhite:0.0 alpha:0.5]; self.toolbar.translucent = YES; 

These lines did my job (ios4.3). As I understand it, setting the translluent property toolbar to YES, you set it to a transparent (transparent) background. Of course, it depends on the color of the shade that you set. If it does not contain an alpha component, the panel will be opaque.

+1
source share

The main problem with controlling the alpha value of a toolbar is that it applies to the entire toolbar and its buttons. If you set backgroundColor with some alpha value <1, the resulting color is combined with barStyle (UIBarStyleDefault or UIBarStyleBlack), which results in a more opaque color than the original backgroundColor . If you set barTintColor , it seems to override all other settings, including any alpha value that you set, which will result in a completely opaque color.

The only way I was able to reduce the alpha of the toolbar without affecting its buttons is to set the background image using setBackgroundImage:forToolbarPosition:barMetrics: You can create a 1px by 1px image with the desired color and alpha:

 + (UIImage *)onePixelImageWithColor:(UIColor *)color { CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); CGContextRef context = CGBitmapContextCreate(NULL, 1, 1, 8, 0, colorSpace, (CGBitmapInfo)kCGImageAlphaPremultipliedFirst); CGContextSetFillColorWithColor(context, color.CGColor); CGContextFillRect(context, CGRectMake(0, 0, 1, 1)); CGImageRef imgRef = CGBitmapContextCreateImage(context); UIImage *image = [UIImage imageWithCGImage:imgRef]; CGImageRelease(imgRef); CGContextRelease(context); CGColorSpaceRelease(colorSpace); return image; } 

[ Swift version ]

Then set the background image:

 [toolbar setBackgroundImage:[self onePixelImageWithColor:[[UIColor blackColor] colorWithAlphaComponent:0.7]] forToolbarPosition:UIBarPositionBottom barMetrics:UIBarMetricsDefault]; 
+1
source share

After many hours evaluating all of the above methods, I found that the method proposed by Ryan X is possible. This is because it is a method that you can manipulate an alpha value, while others cannot.

Here is the version for Swift 3:

 override func viewDidLoad() { super.viewDidLoad() let bgImageColor = UIColor.black.withAlphaComponent(0.6) //you can adjust the alpha value here self.upperToolBar.setBackgroundImage(onePixelImageWithColor(color: bgImageColor), forToolbarPosition: .any, barMetrics: UIBarMetrics.default) } func onePixelImageWithColor(color : UIColor) -> UIImage { let colorSpace = CGColorSpaceCreateDeviceRGB() let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.premultipliedLast.rawValue) var context = CGContext(data: nil, width: 1, height: 1, bitsPerComponent: 8, bytesPerRow: 0, space: colorSpace, bitmapInfo: bitmapInfo.rawValue) context!.setFillColor(color.cgColor) context!.fill(CGRect(x:0,y: 0, width: 1, height:1)) let image = UIImage(cgImage: context!.makeImage()!) return image } 
0
source share

All Articles