UIButton subclass is changing font on click

0

I am seeing some weird behavior from an array of buttons I have built in storyboard. I have 4 buttons each of custom type TakesContainerButton and when a button is clicked it changes to the system font, but when a different button is clicked the previously button returns to the desired font, not sure what is going on here

The buttons are also embedded in a stack view, if that matters

Here is the the implementation when one of the buttons is pressed where buttons is an array of the 4 buttons

@IBAction func filterPressed(_ sender: TakesContainerButton) {
        for button in buttons {
            button.unclick()
        }
        sender.click()
    }

here is the custom class

class TakesContainerButton: UIButton {

        
        var bottom = UIView()
        
        func click(){
            self.setTitleColor(.darkGray, for: .normal)
            let xOffset:CGFloat = 10
            bottom = UIView(frame: CGRect(x: xOffset / 2, y: self.frame.height - 3, width: self.frame.width - xOffset, height: 3))
            bottom.layer.cornerRadius = 1.5
            bottom.backgroundColor = .darkGray
            self.addSubview(bottom)
        }
        
        func unclick(){
            bottom.removeFromSuperview()
            self.setTitleColor(UIColor(hex: "8B8B8B"), for: .normal)
        }
        
        override func awakeFromNib(){
            setFont()
        }
        
        func setFont(){
            self.titleLabel?.font = UIFont(name: "Lato-Bold", size: 12)
        }
    }
ios swift uibutton
2021-11-23 22:12:09
1

0

Is there any specific reason that you are calling setFont() on every click. As I am able to see that you are not changing the font you should set this font at the time of view loading and leave the font as it is.

2021-11-23 22:56:54

Originally It was only in awakeFromNib, but I was experiencing that issue so I put it in both click and unclick
tHatpart

You don't need to set font every time.
Anubhav Giri

In other languages

This page is in other languages

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................