UIButton configuration not being applied when button is interacted with.

I have a problem configuring my UIButton with the new iOS 15 UIButton.Configuration.plain(). I want my button to be sized according to the title label's text using this code:

var configuration = UIButton.Configuration.plain()
configuration.title = title
configuration.titlePadding = 0
configuration.contentInsets = NSDirectionalEdgeInsets(top: 0, leading: 0, bottom: 0, trailing: 0)   

loginButton.configuration = configuration
loginButton.frame = CGRect(x: 0, y: 0, width: 20, height: 14)
loginButton.titleLabel?.font = UIFont(name: "Roboto-Medium", size: 14)

When the app is running on the simulator, the design looks fine but when I tap on the button, the font becomes larger and the font family that I've specified isn't being used as well. The button stays in this configuration state all the way

The same thing occurred when I navigated to another View controller.

Accepted Answer

My bad! I should've looked through the documentation properly. The answer was to set the font using the configuration's attributedTitle property like this:

configuration.attributedTitle = AttributedString(title, attributes: AttributeContainer([NSAttributedString.Key.font : UIFont(name: "Roboto-Medium", size: 12)!]))

I was setting the font size and family like this:

loginButton.titleLabel?.font = UIFont(name: "Roboto-Medium", size: 14)

which was overriding the configuration's default title attributes when the app loaded as we set the font of the titleLabel property after setting the configuration and when the button was interacted with, the configuration's default title attributes were being used.

UIButton configuration not being applied when button is interacted with.
 
 
Q