【问题标题】:iOS - Change only backBarButtonItem text color on a navigation bariOS - 仅更改导航栏上的 backBarButtonItem 文本颜色
【发布时间】:2017-06-27 09:43:51
【问题描述】:

我正在尝试在我的应用 (iOS 9+) 中更改导航栏上的 backBarButtonItem 的颜色。

我可以这样做:

 [[UIBarButtonItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor clearColor]} forState:UIControlStateNormal];

但这会改变导航栏上所有 UIBarButtonItems 的颜色,我只想改变后退按钮。

我试过了:

[self.navigationItem.leftBarButtonItem setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor clearColor]} forState:UIControlStateNormal];

[self.navigationItem.leftBarButtonItem setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor clearColor]} forState:UIControlStateNormal];

但它不起作用

注意:我想保留系统后退按钮的

【问题讨论】:

  • 如果可能,您可以使用自定义视图
  • @MikeAlter 是对的,我们可以使用自定义视图 UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:yourUIButton]
  • 没有办法只改变导​​航栏上特定栏按钮项的颜色吗?
  • 尝试在viewWillAppear 中更改颜色。这个对我有用!在 VC 1 self.navigationController?.navigationBar.tintColor = UIColor.red 和 VC 2 self.navigationController?.navigationBar.tintColor = UIColor.white 中分别给出红色和白色的返回按钮。
  • 回答前请阅读我的问题

标签: ios iphone uinavigationcontroller uinavigationbar uinavigationitem


【解决方案1】:

最后我使用了以下解决方案:

1.设置UIBarbuttonitem的大体外观BEFORE添加右栏按钮项

[[UIBarButtonItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor blueColor]} forState:UIControlStateNormal];

2.然后设置右栏按钮项及其具体外观

UIBarButtonItem *rigthBtn = [[UIBarButtonItem alloc] initWithTitle:@"title" style:UIBarButtonItemStylePlain target:self action:@selector(rightBtnTapped:)];

[rigthBtn setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor redColor]} forState:UIControlStateNormal];

[self.navigationItem setRightBarButtonItem:rigthBtn];

【讨论】:

    【解决方案2】:

    根据我的评论发布示例代码

        UIButton *useButton = [UIButton buttonWithType:UIButtonTypeCustom];
        useButton.frame = CGRectMake(0, 0, 20,17);
        useButton.layer.masksToBounds = NO;
        useButton.layer.shadowOffset = CGSizeMake(3, 3);
        useButton.layer.shadowRadius = 3;
        useButton.layer.shadowOpacity = 0.1;
        useButton.layer.shadowColor = [UIColor blackColor].CGColor;
        useButton.backgroundColor = [UIColor clearColor];
        useButton.tintColor = [UIColor whiteColor];
        [useButton setImage:[UIImage imageNamed:@"image if any"] forState:0];
        [useButton addTarget:self action:@selector(btnBackTapped:) forControlEvents:UIControlEventTouchUpInside];
    
        [self.navigationItem setRightBarButtonItem:[[UIBarButtonItem alloc] initWithCustomView:useButton]];
    

    `

    【讨论】:

    • 这不会使用返回按钮图片
    【解决方案3】:

    晚了一年,但您尝试设置 tintColor 吗?我不知道你的语法,但很快就会是:

    navigationController.navigationBar.tintColor = UIColor.COLOR
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-23
      • 2017-02-17
      • 2014-08-16
      • 1970-01-01
      • 2022-12-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多