繁体   English   中英

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

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

我想在导航栏上设置“完成”按钮的颜色,如下图所示:

在此输入图像描述

虽然,我已将代码设置为self.doneButton.enabled = NO; 但完成按钮仍然是白色。 它不像图像那样改变颜色。

如何设置代码更改文本颜色完成按钮如上图中的完成按钮? 请帮我解决这个问题。 我感谢您的帮助。

用于改变巴顿按钮的颜色

目标C.

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

迅速

或从故事板设置 设置球棒按钮颜色

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

如果你想显示正确的按钮使用

目标C.

self.navigationItem.rightBarButtonItem.enabled = YES;

迅速

self.navigationItem.rightBarButtonItem.enabled = true

如果你想隐藏正确的按钮使用

目标C.

self.navigationItem.rightBarButtonItem.enabled = NO;

迅速

self.navigationItem.rightBarButtonItem.enabled = false

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

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

您可以从XIB / Storyboard中设置它,如图所示 设置栏按钮颜色

或者你可以设置属性 - 你的barbutton项目的tintColor。

尝试这个

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
}

谢谢大家的支持。 我也像你一样改变代码,但“完成”按钮的文字颜色不能改变。 现在,我已经为这个问题找到了一个很好的解决方案。

首先,我创建一个这样的通用函数:

- (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];

}

其次我将它应用到viewDidload中:

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

}

当我应用这些代码时,“完成”按钮已经改变了颜色。

要更新右栏项目文本的颜色,我们应该使用“setTitleTextAttributes”(swift 5)。 请试试这个:

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