skip to Main Content

I can not seem to get the text in two of my buttons to change to white, unless I manually do it through the storyboard. Any ideas?

class AppUIButton: UIButton {

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        
        backgroundColor = Theme.tintColor
        layer.cornerRadius = frame.height / 2
        setTitleColor(UIColor.white, for: .normal)
    }
}


class AddTripViewController: UIViewController {
    
    @IBOutlet weak var titleLabel: UILabel!
    @IBOutlet weak var tripTextField: UITextField!
    @IBOutlet weak var cencelButton: UIButton!
    @IBOutlet weak var saveButton: UIButton!
    
    var doneSaving: (() -> ())?

    override func viewDidLoad() {
        super.viewDidLoad()

        titleLabel.font = UIFont(name: Theme.mainFontName, size: 24)
        
        //I can force the text color to change with the bellow
        //cencelButton.setTitle("Cancel", for: .normal)
        //cencelButton.setTitleColor(UIColor.white, for: .normal)
        
        //saveButton.setTitle("Add", for: .normal)
        //saveButton.setTitleColor(UIColor.white, for: .normal)
       
    }
    
    @IBAction func cancel(_ sender: Any) {
        dismiss(animated: true)
    }
    
    @IBAction func save(_ sender: Any) {
        TripFunctions.createTrip(tripModel: TripModel(title: tripTextField.text!))
        if let doneSaving = doneSaving {
            doneSaving()
        }
        dismiss(animated: true)
    }
}

3

Answers


  1. This should work for you.

    titleLabel.textColor = UIColor.white
    
    Login or Signup to reply.
  2. If you want to create a custom class for UIButton you can do something like this

    import UIKit
    
    @IBDesignable
    class ThemeButton: UIButton {
    
         
        @IBInspectable var borderRadius:CGFloat = 10.0 {
            didSet {
                self.layer.cornerRadius = self.borderRadius
            }
        }
        
        @IBInspectable var fontSize:CGFloat = 14 {
            didSet {
                self.titleLabel?.font = UIFont.ThemeRegular(ofSize: fontSize)
            }
        }
        
        required init?(coder aDecoder: NSCoder) {
            super.init(coder: aDecoder)
            setUI()
            //        self.isSelected = true
        }
        
        override init(frame: CGRect) {
            super.init(frame: frame)
            setUI()
        }
        
        override func prepareForInterfaceBuilder() {
            setUI()
        }
        
        private func setUI() {
            DispatchQueue.main.async {
                self.clipsToBounds = true
                self.setTitleColor(.white, for: .normal)
                self.titleLabel?.font = UIFont.ThemeRegular(ofSize: self.fontSize)
                self.backgorundColor = .red
                self.layer.cornerRadius = self.borderRadius
            }
        }
    }
    

    And you can use it by giving your button class name to the UIButton

    enter image description here

    Login or Signup to reply.
  3. This should work for you:

    yourButton.tintColor = .white
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search