首页 > 解决方案 > 无法将作为自定义视图实现的 UILabel 的文本颜色设置为 UIBarButtonItem

问题描述

我想在工具栏中实现自定义标签,但无法更改其文本颜色:

 // 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();
    }

由于某些原因,白色将被忽略:

在此处输入图像描述

有任何想法吗?

标签: iosswiftxcodeuikit

解决方案


我找到了原因。显然,如果情节提要中未启用 bar 按钮项,则您无法更改颜色:

在此处输入图像描述


推荐阅读