【IOS】自定义UINavigationController看这篇就够了

本文详细介绍了如何自定义UINavigationController,包括改变导航栏背景颜色、设置标题和按钮颜色,以及定制返回按钮文字。通过创建自定义导航控制器类并覆盖特定方法,可以满足大部分APP的导航栏定制需求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这篇文章足以应对90%的APP应用定制化的需求了.

首先我们创建一个类JDCustomNavigationController继承UINavigationController

需要创建UINavigationController的时候只要用JDCustomNavigationController代替即可

默认导航栏应该是这个样子的:


1.修改背景颜色:
在.m中实现如下方法

+ (void)initialize {
    //appearance方法返回一个导航栏的外观对象
    //修改了这个外观对象,相当于修改了整个项目中的外观
    UINavigationBar *navigationBar = [UINavigationBar appearance];
    //设置导航栏背景颜色
    [navigationBar setBarTintColor:JDRGBColor(55,207,240,1)];
}

导航栏将会变成这样:


2.设置导航栏标题颜色

   //设置标题栏颜色
    navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName : [UIFont systemFontOfSize:18]};
    

3.设置导航栏按钮颜色

    //设置NavigationBarItem文字的颜色
  [navigationBar setTintColor:[UIColor whiteColor]];

导航栏就变成这个样子了:


但是我们push其他页面的时候导航栏是这样个样子的


怎么修改返回按钮的文字呢?

-只需要重写下边的方法:

//重写push后返回按钮的文字,文字可以为空字符串.
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    //修改返回文字
    viewController.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStyleDone target:nil action:nil];
  
    [super pushViewController:viewController animated:animated];
}```

就会变成这样子了:

![](http://upload-images.jianshu.io/upload_images/746057-2a5206a1c1aea0a9.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
那如果我们连文字都不想要,想要完全的一个返回箭头或者其他图片怎么办呢?
1.你可以将上边的返回文字改为@"",这样虽然可以,但是返回箭头偏左,不太美观,如下图:
![](http://upload-images.jianshu.io/upload_images/746057-b3a889d865dfcf1a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
那怎么办呢?
干脆重写返回按钮
但是要注意我的写法:
  • (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
    {
    //全部修改返回按钮,但是会失去右滑返回的手势
    if (viewController.navigationItem.leftBarButtonItem ==nil && self.viewControllers.count >=1) {

      viewController.navigationItem.leftBarButtonItem = [self creatBackButton];
    

    }

    [super pushViewController:viewController animated:animated];
    }

-(UIBarButtonItem *)creatBackButton
{
return [[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:@"back"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]style:UIBarButtonItemStylePlain target:self action:@selector(popSelf)];

}
-(void)popSelf
{
[self popViewControllerAnimated:YES];
}

用的是leftBarButtonItem而不是backBarButtonItem哦,具体区别请看这里
[leftBarButtonItem与backBarButtonItem的区别](http://www.jianshu.com/p/43dad6794db4)

if (viewController.navigationItem.leftBarButtonItem ==nil && self.viewControllers.count >=1)

这个判断是为了最底层的viewcontroller不至于加上我们自定义的按钮
运行之后的效果:

![](http://upload-images.jianshu.io/upload_images/746057-df5f999b089f9a4c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

但是有个我们,我发现系统的右滑返回失效了,怎么办呢?
别着急,只要在viewDidload中添加以下方法即可

//重写了leftbarItem之后,需要添加如下方法才能重新启用右滑返回
if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
self.interactivePopGestureRecognizer.delegate = nil;
}

这样足够使用了吧.

另外附隐藏某个导航栏背景透明的方法
  • (void)viewWillAppear:(BOOL)animated
    {
    [super viewWillAppear:animated];
    //设置导航栏背景图片为一个空的image,这样就透明了
    [self.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
    //去掉透明后导航栏下边的黑边
    [self.navigationBar setShadowImage:[[UIImage alloc] init]];
    }

  • (void)viewWillDisappear:(BOOL)animated{

    //如果不想让其他页面的导航栏变为透明 需要重置
    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:nil];
    }


其他小知识点:
//设置导航栏文字的主题
 NSShadow *shadow = [[NSShadow alloc]init];
 [shadow setShadowOffset:CGSizeZero];
 [navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor],NSShadowAttributeName : shadow}];
 [navigationBar setBackgroundImage:[UIImage imageNamed:@"ic_cell_bg_selected"] forBarMetrics:UIBarMetricsDefault];
 //修改所有UIBarButtonItem的外观
 UIBarButtonItem *barButtonItem = [UIBarButtonItem appearance];
 
 // 修改item的背景图片
 //[barItem setBackgroundImage:[UIImage imageNamed:@"navigationbar_button_background.png"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
 //[barItem setBackgroundImage:[UIImage imageNamed:@"navigationbar_button_background_pushed.png"] forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
 //修改item上面的文字样式
 NSDictionary *dict =@{NSForegroundColorAttributeName : [UIColor whiteColor],NSShadowAttributeName : shadow};
 [barButtonItem setTitleTextAttributes:dict forState:UIControlStateNormal];
 [barButtonItem setTitleTextAttributes:dict forState:UIControlStateHighlighted];
 //修改返回按钮样式
 [barButtonItem setBackButtonBackgroundImage:[UIImage imageNamed:NAVIGATION_BAR_BACK_ICON_NAME] forState:UIControlStateNormal barMetrics:UIBarMetricsCompact];
 //设置状态栏样式
 [[UIApplication sharedApplication]setStatusBarStyle:UIStatusBarStyleLightContent];

Demo地址:https://github.com/yuying2012/WJDStudyLibrary
这是一个大工程,请从工程中寻找相关模块代码.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值