简体   繁体   English

如何在导航栏上设置颜色`rightBarButtonItem`?

[英]How to set color `rightBarButtonItem` on navigation bar?

I want to set color of "Done" button on navigation bar like this image: 我想在导航栏上设置“完成”按钮的颜色,如下图所示:

在此输入图像描述

Although, I've set code as self.doneButton.enabled = NO; 虽然,我已将代码设置为self.doneButton.enabled = NO; but Done button still has a white color. 但完成按钮仍然是白色。 It does not change color like image. 它不像图像那样改变颜色。

How to set the code to change text color done button like Done button in the image above? 如何设置代码更改文本颜色完成按钮如上图中的完成按钮? Please help me to solve this problem. 请帮我解决这个问题。 I appreciate your help. 我感谢您的帮助。

for change the color of barbutton use 用于改变巴顿按钮的颜色

objective C 目标C.

self.navigationItem.rightBarButtonItem = yourRightbarbuttonName;
self.navigationItem.rightBarButtonItem.tintColor = [UIColor blueColor]; // add your color

Swift 迅速

or set from Story board 或从故事板设置 设置球棒按钮颜色

self.navigationItem.rightBarButtonItem = yourRightbarbuttonName
self.navigationItem.rightBarButtonItem.tintColor = UIColor.blueColor()

if you want to show the Right barbutton use 如果你想显示正确的按钮使用

objective C 目标C.

self.navigationItem.rightBarButtonItem.enabled = YES;

Swift 迅速

self.navigationItem.rightBarButtonItem.enabled = true

if you want to hide the Right barbutton use 如果你想隐藏正确的按钮使用

objective C 目标C.

self.navigationItem.rightBarButtonItem.enabled = NO;

Swift 迅速

self.navigationItem.rightBarButtonItem.enabled = false

禁用按钮后添加以下行可能会有所帮助(尽管未测试)

self.navigationItem.rightBarButtonItem.tintColor = [UIColor lightGrayColor];

You can set it from XIB/Storyboard like shown in image 您可以从XIB / Storyboard中设置它,如图所示 设置栏按钮颜色

or you can set the property - tintColor of your barbutton item. 或者你可以设置属性 - 你的barbutton项目的tintColor。

Try This 尝试这个

UIBarButtonItem *rightBarButton=[[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStylePlain target:self action:@selector(editProfileClick)];
rightBarButton.tintColor=[UIColor whiteColor];
self.navigationItem.rightBarButtonItem = rightBarButton;


-(void)editProfileClick
{
//your methods
}

Thanks guys for support me. 谢谢大家的支持。 I also change the code like you but text color of "Done" button can not change. 我也像你一样改变代码,但“完成”按钮的文字颜色不能改变。 Now, I've found a good solution for this issue. 现在,我已经为这个问题找到了一个很好的解决方案。

Firstly, I create a common function like that: 首先,我创建一个这样的通用函数:

- (void)changeStatusOfRightBarButton:(BOOL)enabled withColorAlpha:(CGFloat)numberAlpha {
    _doneButton.enabled = enabled;
    [[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil]
    setTitleTextAttributes:
    @{NSForegroundColorAttributeName:[UIColor colorWithHexString:@"FFFFFF" alpha:numberAlpha],
    NSFontAttributeName:[UIFont fontAvenirNextRegularWithSize:15.0f]
    }
    forState:UIControlStateNormal];

} }

and secondly I apply it into viewDidload like that: 其次我将它应用到viewDidload中:

if (self.noteTextView.text.length == 0) {
    [self.noteTextView becomeFirstResponder];
    [self changeStatusOfRightBarButton:NO withColorAlpha:0.44f];

} }

When I apply these code "Done" button has changed color. 当我应用这些代码时,“完成”按钮已经改变了颜色。

For updated the colour for text of right bar item, we should use "setTitleTextAttributes"(swift 5). 要更新右栏项目文本的颜色,我们应该使用“setTitleTextAttributes”(swift 5)。 Please try this : 请试试这个:

let rightBarButton = UIBarButtonItem(title: "your text", style: .plain, target: nil, action: nil)
rightBarButton.setTitleTextAttributes([NSAttributedString.Key.foregroundColor : UIColor.black], for: .normal)
navigationItem.rightBarButtonItem = rightBarButton

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM