I wrote the following code to make my toolbar transparent.
[mtoolbar setBackgroundColo开发者_如何学Pythonr:[UIColor clearColor]];
How do I make UIToolbar
transparent?
You can set the property translucent
to YES
and see if this helps.
[self.toolbar setBackgroundImage:[UIImage new]
forToolbarPosition:UIToolbarPositionAny
barMetrics:UIBarMetricsDefault];
[self.toolbar setBackgroundColor:[UIColor clearColor]];
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];
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
for (UIView * sv in [toolBar subviews])
{
[sv removeFromSuperview];
}
;) any iOs
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];
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];
精彩评论