簡體   English   中英

無法將作為自定義視圖實現的 UILabel 的文本顏色設置為 UIBarButtonItem

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

我想在工具欄中實現自定義 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();
    }

由於某些原因,白色將被忽略:

在此處輸入圖像描述

有任何想法嗎?

嘗試為 UILable 設置色調顏色,或者您也可以使用 UIButton

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

rightBarButtonItem.tintColor = UIColor.myMusicPurple 

我找到了原因。 顯然,如果在 storyboard 中未啟用條形按鈕項,則無法更改顏色:

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM