Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIButton subclass is changing font on click

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)
        }
    }
like image 942
tHatpart Avatar asked Nov 07 '25 01:11

tHatpart


1 Answers

In StoryBoard set button :

Type to Custom

Style to Default and

State Config to Default

you should have something like this

enter image description here

like image 188
THEJAS Avatar answered Nov 09 '25 17:11

THEJAS