+ (void)initialize
{
UINavigationBar *bar = [UINavigationBar appearance];
// bar.clipsToBounds = YES;
// [bar setBackgroundImage:[UIImage imageNamed:@"navigation"] forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setShadowImage:[[UIImage alloc] init]];
// 设置整个项目所有item的主题样式
UIBarButtonItem *item = [UIBarButtonItem appearance];
// 设置普通状态
// key:NS****AttributeName
NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
textAttrs[NSForegroundColorAttributeName] = Color(0, 0, 0, 10);
textAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:17.0];
[item setTitleTextAttributes:textAttrs forState:UIControlStateNormal];
[bar setTitleTextAttributes:textAttrs];
// 设置不可用状态
NSMutableDictionary *disableTextAttrs = [NSMutableDictionary dictionary];
disableTextAttrs[NSForegroundColorAttributeName] = [UIColor colorWithRed:0.6 green:0.6 blue:0.6 alpha:0.7];
disableTextAttrs[NSFontAttributeName] = textAttrs[NSFontAttributeName];
[item setTitleTextAttributes:disableTextAttrs forState:UIControlStateDisabled];
//设置导航栏字体的颜色
[[UINavigationBar appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys: Color(0, 0, 0, 10), NSForegroundColorAttributeName, [UIFont boldSystemFontOfSize:17], NSFontAttributeName, nil]];
设置导航栏背景的颜色
[[UINavigationBar appearance] setTintColor:Color(0, 0, 0, 10)];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.navigationController.navigationBar setBarStyle:UIBarStyleBlack];
}
- (UIStatusBarStyle)preferredStatusBarStyle
{
return UIStatusBarStyleLightContent;
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
//只有在二级页面生效
if ([self.navigationController.viewControllers count] == 2) {
// self.navigationController.interactivePopGestureRecognizer.delegate = self;
}
}
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
// UIImage* image = [UIImage imageNamed:@"Navigation_backBtn"];
// [item setBackButtonBackgroundImage:[image resizableImageWithCapInsets:UIEdgeInsetsMake(0, image.size.width, 0, 0)] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
//
// [item setBackButtonTitlePositionAdjustment:UIOffsetMake(-400.f, 0) forBarMetrics:UIBarMetricsDefault];
if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
self.interactivePopGestureRecognizer.delegate = nil;
}
// [self.navigationItem.backBarButtonItem setBackButtonBackgroundImage:[image resizableImageWithCapInsets:UIEdgeInsetsMake(0, image.size.width, 0, 0)] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
// [self.navigationItem.backBarButtonItem setBackButtonTitlePositionAdjustment:UIOffsetMake(-400.f, 0) forBarMetrics:UIBarMetricsDefault];
}
/**
* 重写这个方法目的:能够拦截所有push进来的控制器
*
* @param viewController 即将push进来的控制器
*/
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
if (self.viewControllers.count > 0) { // 这时push进来的控制器viewController,不是第一个子控制器(不是根控制器)
/* 自动显示和隐藏tabbar */
viewController.hidesBottomBarWhenPushed = YES;
/* 设置导航栏上面的内容 */
// 设置左边的返回按钮
// viewController.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithImageName:@"Navigation_backBtn" highImageName:@"Navigation_backBtn" target:self action:@selector(back) title:nil];
// self.navigationController.interactivePopGestureRecognizer.delegate = self;
// [viewController.navigationItem.backBarButtonItem setBackButtonBackgroundImage:[UIImage imageNamed:@"navigationbar_more"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
// 设置右边的更多按钮
// viewController.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithTarget:self action:@selector(more) image:@"navigationbar_more" highImage:@"navigationbar_more_highlighted"];
}
[super pushViewController:viewController animated:animated];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - View rotation
- (BOOL)shouldAutorotate {
return NO;
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return UIInterfaceOrientationPortrait;
}
- (void)back {
// 因为self本来就是一个导航控制器,self.navigationController这里是nil的
[self popViewControllerAnimated:YES];
}
自定义导航栏(Base)
最新推荐文章于 2023-08-05 20:26:49 发布