// 状态栏高度
CGFloat statusBarHeight = [[UIApplication sharedApplication] statusBarFrame].size.height;
// 导航栏高度
CGFloat navBarHeight = self.navigationController.navigationBar.frame.size.height;
X以前: 20,44
x以及以后:44,44
//状态栏和导航条高度
#define getRectNavAndStatusHight self.navigationController.navigationBar.frame.size.height+[[UIApplication sharedApplication] statusBarFrame].size.height
x 底部高度:34
二。
创建一个UINavigationController
@interface ABNavigationController : UINavigationController
1.统一设置title颜色和大小
self.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName:[UIFont boldSystemFontOfSize:17]};
2.统一设置导航条
#pragma mark 重写push方法 导航栏按钮
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
// if (self.pushing == YES) {
// NSLog(@"被拦截");
// return;
// } else {
// NSLog(@"push");
// self.pushing = YES;
// }
if (self.childViewControllers.count > 0) { // 如果push进来的不是第一个控制器
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
//[button setTitle:@"返回" forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:@"问题反馈"] forState:UIControlStateNormal];
// [button setImage:[UIImage imageNamed:@"刷新"] forState:UIControlStateHighlighted];
button.size = CGSizeMake(70, 30);
//下面两行设置图片位置和大小
[button setImageEdgeInsets:UIEdgeInsetsMake(5, -5, 5, 0)];//上左下右
button.imageView.contentMode = UIViewContentModeScaleAspectFit;
// 让按钮内部的所有内容左对齐
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
// [button sizeToFit];
// 让按钮的内容往左边偏移10
// button.contentEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 0);//上左下右
// [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
// [button setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
[button addTarget:self action:@selector(popViewControllerAnimated:) forControlEvents:UIControlEventTouchUpInside];
[button setBackgroundColor:[UIColor redColor]];
// 修改导航栏左边的item
viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
// 隐藏tabbar
viewController.hidesBottomBarWhenPushed = YES;
}
// 这句super的push要放在后面, 让viewController可以覆盖上面设置的leftBarButtonItem
// 意思是,我们任然可以重新在push控制器的viewDidLoad方法中设置导航栏的leftBarButtonItem,如果设置了就会覆盖在push方法中设置的“返回”按钮,因为 [super push....]会加载push的控制器执行viewDidLoad方法。
[super pushViewController:viewController animated:animated];
}
UINavigationBar透明设置
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[self.navigationController.navigationBar setTranslucent:YES];//设置透明度
// //颜色字体
self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:kWhiteColor,NSFontAttributeName:[UIFont boldSystemFontOfSize:17]};
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:@"icon_back"] forState:UIControlStateNormal];
button.size = CGSizeMake(70, 44);
[button setImageEdgeInsets:UIEdgeInsetsMake(5, 5, 5, 0)];//上左下右
button.imageView.contentMode = UIViewContentModeScaleAspectFit;
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
[button addTarget:self action:@selector(backBtn) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
//状态栏
//1.在info.plist中添加View controller-based status bar appearance,值为NO
[UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
}
- (void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
[self.navigationController.navigationBar setTranslucent:NO];//设置透明度
[UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;//状态栏
//颜色字体
self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:kNavTitleColor,NSFontAttributeName:[UIFont boldSystemFontOfSize:17]};
}
网友评论