Skip to content

Instantly share code, notes, and snippets.

@yelled3
Last active August 23, 2017 15:15
Show Gist options
  • Save yelled3/6881965 to your computer and use it in GitHub Desktop.
Save yelled3/6881965 to your computer and use it in GitHub Desktop.
UINavigationBar with totally transparent background (iOS6 + iOS7 tested)
#import <UIKit/UIKit.h>
@interface MyCustomNavigationBar : UINavigationBar
@end
@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
@dannyshmueli
Copy link

line 46: float -> CGFloat

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment