I would like an entirely transparent UIToolbar
and/or UINavigationBar
. I have tried the various incantations suggested for pre- and post-iOS 5 but none seem to work any more.
How might this be accomplished in iOS 7?
I would like an entirely transparent UIToolbar
and/or UINavigationBar
. I have tried the various incantations suggested for pre- and post-iOS 5 but none seem to work any more.
How might this be accomplished in iOS 7?
UIToolbar
self.toolbar.setBackgroundImage(UIImage(),
forToolbarPosition: .any,
barMetrics: .default)
self.toolbar.setShadowImage(UIImage(), forToolbarPosition: .any)
UINavigationBar
self.navigationBar.setBackgroundImage(UIImage(), for: .default)
self.navigationBar.shadowImage = UIImage()
self.navigationBar.isTranslucent = true
UIToolbar
self.toolbar.setBackgroundImage(UIImage(),
forToolbarPosition: UIBarPosition.Any,
barMetrics: UIBarMetrics.Default)
self.toolbar.setShadowImage(UIImage(),
forToolbarPosition: UIBarPosition.Any)
UINavigationBar
self.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: UIBarMetrics.Default)
self.navigationBar.shadowImage = UIImage()
self.navigationBar.translucent = true
UIToolbar
[self.toolbar setBackgroundImage:[UIImage new]
forToolbarPosition:UIBarPositionAny
barMetrics:UIBarMetricsDefault];
[self.toolbar setShadowImage:[UIImage new]
forToolbarPosition:UIBarPositionAny];
UINavigationBar
[self.navigationBar setBackgroundImage:[UIImage new]
forBarMetrics:UIBarMetricsDefault];
self.navigationBar.shadowImage = [UIImage new];
self.navigationBar.translucent = YES;
Setting translucent
to YES
on the navigation bar does the trick, due to a behavior discussed in the UINavigationBar
documentation. I'll report here the relevant fragment:
If you set this property to
YES
on a navigation bar with an opaque custom background image, the navigation bar will apply a system opacity less than 1.0 to the image.
iOS 7
simulator –
Bedtime translucent
value of UIToolbar
to YES
/NO
and it didn't affect the final result. Are you instantianing the toolbar from code or using a xib/storyboard? –
Bedtime UIToolbarPositionAny
instead of UIBarPositionAny
for the transparent UIToolbar
? –
Contrapuntist UIToolbarPosition
is deprecated in favor of UIBarPosition
since iOS 6.1 –
Bedtime UIToolbarPosition
is just a macro 2) Then why are you using in the next line of code? –
Contrapuntist UIToolbarPosition
is deprecated. –
Bedtime If you want to do it through the entire app you should use the UIAppearance proxy (iOS5+):
UINavigationBar *navigationBarAppearance = [UINavigationBar appearance];
navigationBarAppearance.backgroundColor = [UIColor clearColor];
[navigationBarAppearance setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
navigationBarAppearance.shadowImage = [[UIImage alloc] init];
Article: http://nshipster.com/uiappearance/
UINavigationController
subclasses—i.e., the ones that you want this behavior applied to. –
Mayne Try:
[navBar setBackgroundImage:[UIImage alloc] forBarMetrics:UIBarMetricsDefault];
@implementation MyCustomNavigationBar
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setup];
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
[self setup];
}
return self;
}
- (void)setup {
[self setupBackground];
}
- (void)setupBackground {
self.backgroundColor = [UIColor clearColor];
self.tintColor = [UIColor clearColor];
// make navigation bar overlap the content
self.translucent = YES;
self.opaque = NO;
// remove the default background image by replacing it with a clear image
[self setBackgroundImage:[self.class maskedImage] forBarMetrics:UIBarMetricsDefault];
// remove defualt bottom shadow
[self setShadowImage: [UIImage new]];
}
+ (UIImage *)maskedImage {
const float colorMask[6] = {222, 255, 222, 255, 222, 255};
UIImage *img = [UIImage imageNamed:@"nav-white-pixel-bg.jpg"];
return [UIImage imageWithCGImage: CGImageCreateWithMaskingColors(img.CGImage, colorMask)];
}
@end
Something I stumbled upon is that if I created a subclassed UINavigationBar
and then created an empty -(void)drawRect:
method, I would get a transparent navigation bar. I only tested this in iOS 7.*, but it seemed to work!
© 2022 - 2024 — McMap. All rights reserved.