Skip to content

Instantly share code, notes, and snippets.

@liufsd
Forked from aprato/AMPNavigationBar.m
Created September 25, 2013 09:52
Show Gist options
  • Save liufsd/6697451 to your computer and use it in GitHub Desktop.
Save liufsd/6697451 to your computer and use it in GitHub Desktop.
// .h
@interface AMPNavigationBar : UINavigationBar
@end
// .m
@interface AMPNavigationBar ()
@property (nonatomic, strong) CALayer *extraColorLayer;
@end
static CGFloat const kDefaultColorLayerOpacity = 0.5f;
static CGFloat const kSpaceToCoverStatusBars = 100.0f;
@implementation AMPNavigationBar
- (void)setBarTintColor:(UIColor *)barTintColor
{
[super setBarTintColor:barTintColor];
if (self.extraColorLayer == nil) {
self.extraColorLayer = [CALayer layer];
self.extraColorLayer.opacity = kDefaultColorLayerOpacity;
[self.layer addSublayer:self.extraColorLayer];
}
self.extraColorLayer.backgroundColor = barTintColor.CGColor;
}
- (void)layoutSubviews
{
[super layoutSubviews];
if (self.extraColorLayer != nil) {
self.extraColorLayer.frame = CGRectMake(0, 0 - kSpaceToCoverStatusBars, CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds) + kSpaceToCoverStatusBars);
}
}
@end
// the easy way to use it is to subclass UINavigationController and override:
- (id)initWithRootViewController:(UIViewController *)rootViewController
{
self = [super initWithNavigationBarClass:[APNavigationBar class] toolbarClass:[UIToolbar class]];
if (self) {
self.viewControllers = @[ rootViewController ];
}
return self;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment