Created
January 7, 2014 10:09
-
-
Save tonycn/8297307 to your computer and use it in GitHub Desktop.
Customise top bar with base controller
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@interface TopBarViewController () | |
@property (nonatomic, strong) UIView *statusbarBgView; | |
@end | |
@implementation TopBarViewController | |
- (void)viewDidLoad | |
{ | |
[super viewDidLoad]; | |
// Do any additional setup after loading the view. | |
if (OS_PRIOR_IOS_7 && ![UIApplication sharedApplication].statusBarHidden) { | |
CGFloat statusBarH = [UIApplication sharedApplication].statusBarFrame.size.height; | |
CGRect f = (CGRect){0.f, 0.f, self.view.frame.size.width, statusBarH}; | |
self.statusbarBgView = [[UIView alloc] initWithFrame:f]; | |
self.statusbarBgView.autoresizingMask = UIViewAutoresizingFlexibleWidth; | |
self.statusbarBgView.backgroundColor = [UIColor clearColor]; | |
[self.view addSubview:self.statusbarBgView]; | |
} | |
} | |
- (void)setBodyView:(UIView *)bodyView | |
{ | |
[_bodyView removeFromSuperview]; | |
_bodyView = bodyView; | |
[self.view addSubview:_bodyView]; | |
if (self.topBar) { | |
[self.view bringSubviewToFront:self.topBar]; | |
[self.view bringSubviewToFront:self.statusbarBgView]; | |
} | |
[self layoutBodyViewToFit]; | |
} | |
- (void)setTopBar:(UIView *)topBar | |
{ | |
[_topBar removeFromSuperview]; | |
_topBar = topBar; | |
if (OS_PRIOR_IOS_7) { | |
CGFloat statusBarH = [UIApplication sharedApplication].statusBarFrame.size.height; | |
[_topBar app_setOrigY:statusBarH]; | |
self.statusbarBgView.backgroundColor = _topBar.backgroundColor; | |
} | |
[self.view addSubview:_topBar]; | |
[self layoutBodyViewToFit]; | |
} | |
- (void)setTopBarTranslucent:(BOOL)topBarTranslucent | |
{ | |
_topBarTranslucent = topBarTranslucent; | |
[self layoutBodyViewToFit]; | |
} | |
- (void)layoutBodyViewToFit | |
{ | |
if (self.bodyView) { | |
CGRect rect = self.view.bounds; | |
if (!self.topBarHidden && !self.topBarTranslucent && _topBar) { | |
rect.origin.y = CGRectGetMaxY(_topBar.frame); | |
} | |
rect.size.height -= rect.origin.y; | |
self.bodyView.frame = rect; | |
self.bodyView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth; | |
} | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment