Search code examples
iosswiftxcodeuibutton

Custom UIButton -> part of title disappears after click


I created custom button subclassing UIButton. I set custom font for titleLabel.

After tapping on custom button, the part of title disappears.

Without setting custom font, the text stays the same.

I think this problem only occures with devices targeting ios >= 13.

Before tap -> enter image description here

After tap ->enter image description here

//BasicButton class

class BasicButton: UIButton {
    


    override init(frame: CGRect) {
        super.init(frame: frame)
        titleLabel?.font = UIFont.proximaNovaBold.withSize(20.0)
        layoutIfNeeded()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        
        titleLabel?.font = UIFont.proximaNovaBold.withSize(20.0)
        layoutIfNeeded()
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        titleLabel?.font = UIFont.proximaNovaBold.withSize(20.0)
        layoutIfNeeded()
        }
    
    override func setTitle(_ title: String?, for state: UIControl.State) {
        super.setTitle(title, for: state)
        titleLabel?.font = UIFont.proximaNovaBold.withSize(20.0)
        layoutIfNeeded()
    }
}

//extension UIFont

@nonobjc class var proximaNovaBold: UIFont {
        return UIFont(name: "ProximaNova-Bold", size: 17.0)!
    }

//storyboard enter image description here

//BasicButton storyboard properties

enter image description here] enter image description here


Solution

  • changing Style of the button from Plain to Default was solution for the problem