Wednesday, April 22, 2026
HomeiOS Developmentswift - Customized iOS UIButton Sub-Class Not Updating Title

swift – Customized iOS UIButton Sub-Class Not Updating Title

[ad_1]

I’ve a subclass of a UIButton that takes the title label, and places it underneath the button’s picture, versus the correct of the picture:

last class ImageButton: UIButton {
    @IBInspectable var cornerRadius: CGFloat = 8
    @IBInspectable var borderColor: UIColor? = .black
    
    personal enum Constants {
        static let imageSize: CGFloat = 40
        static let titleHeight: CGFloat = 12
    }
    
    override func titleRect(forContentRect contentRect: CGRect) -> CGRect {
        if #out there(iOS 15, *) {
            return tremendous.titleRect(forContentRect: contentRect)
        }
        else {
            _ = tremendous.titleRect(forContentRect: contentRect)
            return CGRect(
                x: 0,
                y: contentRect.peak - Constants.titleHeight,
                width: contentRect.width,
                peak: Constants.titleHeight
            )
        }
    }
    
    override func imageRect(forContentRect contentRect: CGRect) -> CGRect {
        if #out there(iOS 15, *) {
            return tremendous.imageRect(forContentRect: contentRect)
        } else {
            return CGRect(
                x: contentRect.width / 2 - Constants.imageSize / 2,
                y: (contentRect.peak - titleRect(forContentRect: contentRect).peak) / 2 - Constants.imageSize / 2,
                width: Constants.imageSize,
                peak: Constants.imageSize
            )
        }
    }
    
    override var intrinsicContentSize: CGSize {
        
        if #out there(iOS 15, *) {
            return tremendous.intrinsicContentSize
        }
        else {
            _ = tremendous.intrinsicContentSize
            let dimension = titleLabel?.sizeThatFits(contentRect(forBounds: bounds).dimension) ?? .zero
            let spacing: CGFloat = 12
            return CGSize(
                width: max(dimension.width, Constants.imageSize),
                peak: Constants.imageSize + Constants.titleHeight + spacing
            )
        }
    }
    
    override init(body: CGRect) {
        tremendous.init(body: body)
        setup()
    }
    
    required init?(coder aDecoder: NSCoder) {
        tremendous.init(coder: aDecoder)
        setup()
    }
    
    personal func setup() {
        if #out there(iOS 15, *) {
            var myConfiguration = UIButton.Configuration.plain()
            myConfiguration.imagePlacement = .prime
            self.configuration = myConfiguration
        } else {
            titleLabel?.textAlignment = .heart
        }
    }
    override func draw(_ rect: CGRect) {
        layer.cornerRadius = cornerRadius
        layer.masksToBounds = true
        layer.borderWidth = 1
        layer.borderColor = borderColor?.cgColor
    }
}

Attempting to alter the button’s title doesn’t have any impact:

myCustomButton.setTitle("Disable Field Choose", for: .regular)

I attempted including:

myCustomButton.layer.setNeedsLayout()
myCustomButton.layer.setNeedsDisplay()

However, nothing appears to alter the title of myCustomButton

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments