skip to Main Content

I’d like to implement a custom label in the toolbar, but I can’t change its text color:

 // 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:

enter image description here

Any ideas?

2

Answers


  1. Chosen as BEST ANSWER

    I found the reason. Obviously you can't change the color if the bar buttom item is not enabled in the storyboard:

    enter image description here


  2. Try setting tint color for UILable, or you can also use UIButton

    let rightBarButtonItem = UIBarButtonItem(title: "Some text", style: .plain, target: self, action: #selector(someAction))
    
    rightBarButtonItem.tintColor = UIColor.myMusicPurple 
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search