iOS 导航栏的设置

在开发过程中难免会对导航栏进行设置,整理部分导航栏的设置,以备后用

+ (void)initialize {    //appearance方法返回一个导航栏的外观对象    //修改了这个外观对象,相当于修改了整个项目中的外观   

   UINavigationBar *navigationBar = [UINavigationBar appearance];   

 [navigationBar setBarTintColor:[UIColor colorWithRed:0.91 green:0.12 blue:0.31 alpha:1.0]];    [navigationBar setBackgroundImage:[UIImage imageNamed:@"backimg"] forBarMetrics:UIBarMetricsCompact];    

navigationBar.translucent = NO;   //去掉导航栏的半透明状态  (不去会有色差)  

 [[UINavigationBar appearance] setBackgroundImage:[[UIImage alloc] init] forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];  

  [[UINavigationBar appearance] setShadowImage:[[UIImage alloc] init]];    

  [navigationBar setBarStyle:UIBarStyleDefault];//  

  [navigationBar setBarTintColor:[UIColor redColor]];  

  [navigationBar setTintColor:[UIColor whiteColor]];   

 // 设置NavigationBarItem文字的颜色       

 NSShadow *shadow = [[NSShadow alloc]init];  

  [shadow setShadowOffset:CGSizeZero];   

 [navigationBar setTitleTextAttributes:@{  NSForegroundColorAttributeName : [UIColor whiteColor],      NSFontAttributeName : [UIFont systemFontOfSize:18],                                           NSShadowAttributeName : shadow  }];      

  //修改所有UIBarButtonItem的外观   

 UIBarButtonItem *barButtonItem = [UIBarButtonItem appearance];      

  [barButtonItem setTintColor:[UIColor whiteColor]];    //    self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;}

//如果想要统一定制返回按钮样式的话,可以重写如下方法//重写返回按钮

-(void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{  

  [super pushViewController:viewController animated:animated];   

 if (viewController.navigationItem.leftBarButtonItem ==nil && self.viewControllers.count >1) {        viewController.navigationItem.leftBarButtonItem = [self creatBackButton];        self.interactivePopGestureRecognizer.delegate = (id)self;

}

}

-(UIBarButtonItem *)creatBackButton

{

return [[UIBarButtonItem alloc]initWithImage: [UIImage imageNamed:@"back"] style:UIBarButtonItemStylePlain target:self action:@selector(popSelf)];

}

-(void)popSelf

{

[self popViewControllerAnimated:YES];

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容