简体   繁体   English

无法将作为自定义视图实现的 UILabel 的文本颜色设置为 UIBarButtonItem

[英]Can't set text color of UILabel implemented as custom view to UIBarButtonItem

I'd like to implement a custom label in the toolbar, but I can't change its text color:我想在工具栏中实现自定义 label ,但我无法更改其文本颜色:

 // MARK: - Initialize the status label (toolbar)
    func initStatusLabel() {
        statusLabel.layer.borderColor = UIColor.red.cgColor;
        statusLabel.layer.borderWidth = 1;
        statusLabel.layer.masksToBounds = true;
        statusLabel.layer.cornerRadius = 8;
        statusLabel.font = statusLabel.font.withSize(12);
        statusLabel.leftTextInset = 8;
        statusLabel.rightTextInset = 8;
        statusLabel.topTextInset = 8;
        statusLabel.bottomTextInset = 8;
        statusLabel.text = "Hello World";
        statusLabel.adjustsFontSizeToFitWidth = true;
        statusLabel.backgroundColor = .systemRed;
        statusLabel.textColor = .white;
        statusLabel.sizeToFit();
    }

The white color will be ignored for some reasons:由于某些原因,白色将被忽略:

在此处输入图像描述

Any ideas?有任何想法吗?

Try setting tint color for UILable, or you can also use UIButton尝试为 UILable 设置色调颜色,或者您也可以使用 UIButton

let rightBarButtonItem = UIBarButtonItem(title: "Some text", style: .plain, target: self, action: #selector(someAction))

rightBarButtonItem.tintColor = UIColor.myMusicPurple 

I found the reason.我找到了原因。 Obviously you can't change the color if the bar buttom item is not enabled in the storyboard:显然,如果在 storyboard 中未启用条形按钮项,则无法更改颜色:

在此处输入图像描述

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

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