自定义导航按钮

最简单去掉back字样,保留原图标,修改图标颜色为黑色

    self.navigationController.navigationBar.topItem.title = @"";
    [self.navigationController.navigationBar setTintColor:UIColor.blackColor];

只带文字的导航按钮

注意,自定义按钮后,手势滑动返回将失效。如果需要恢复手势滑动返回,需要遵循 <UIGestureRecognizerDelegate> 协议

// 设置自定义的导航按钮
- (void)customNavigationItem{
     UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(gotoViewController)];
    // 字体颜色
    [item setTintColor:UIColor.blackColor];
    self.navigationItem.leftBarButtonItem = item;
    // 恢复手势滑动返回
    self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    self.navigationController.interactivePopGestureRecognizer.delegate = self;
}

// 跳转视图
- (void)gotoViewController{
    [self.navigationController popViewControllerAnimated:YES];
}

只带图片的导航按钮

// 设置自定义的导航按钮
- (void)customNavigationItem{
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"rule_navbar_icon_ruturn"] style:UIBarButtonItemStylePlain target:self action:@selector(gotoViewController)];
    // 设置图片的底色
    [item setTintColor:UIColor.blackColor];
    self.navigationItem.leftBarButtonItem = item;
    // 恢复手势滑动返回
    self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    self.navigationController.interactivePopGestureRecognizer.delegate = self;
}

// 返回上级视图
- (void)gotoViewController{
    [self.navigationController popViewControllerAnimated:YES];
}

带图文的导航按钮

// 自定义图文返回按钮
- (void)customNavigationItem {
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, 44, 44);
    [button setImage:[UIImage imageNamed:@"rule_navbar_icon_ruturn"] forState:UIControlStateNormal];
    [button setTitle:@" 返回" forState:UIControlStateNormal];
    [button setTitleColor:UIColor.blackColor forState:UIControlStateNormal];
    [button addTarget:self action:@selector(gotoViewController) forControlEvents:UIControlEventTouchDown];
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.leftBarButtonItem = item;
    // 保留手势,遵循 <UIGestureRecognizerDelegate>
    self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    self.navigationController.interactivePopGestureRecognizer.delegate = self;
}

- (void)gotoViewController{
    [self.navigationController popViewControllerAnimated:YES];
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1、通过CocoaPods安装项目名称项目信息 AFNetworking网络请求组件 FMDB本地数据库组件 SD...
    阳明AGI阅读 16,018评论 3 119
  • 2017年4月30日,星期日,惠州,晴 七年了,我又回到这里。怎么样走又怎么样回来,物是人非,一切都不一样了。 七...
    晨沐子晞阅读 94评论 0 0
  • 季大爷打来电话的时候,我忙的没有空去接。沐沐睡醒了,在大声的哭,估计是饿了。 孩子总是天真,给他买了套衣服...
    木糖糖阅读 271评论 0 0
  • 在继承UITabBarController的类中实现以下方法 -(void)viewWillLayoutSubvi...
    贺乾龙阅读 262评论 0 0
  • 贰月十六号.过完了情人节。也是返乡后的第十五天。 本身还记得当初打包行李的夜晚还在想着,第二天得回家了?不知道为什...
    初眸阅读 239评论 0 0