UIButton(configuration:... Does not work

I'm trying to use the new uibutton configuration and it doesn't receive touches. What am I missing? Adding other buttons the "old fashioned" way work just fine.

var configuration = UIButton.Configuration.filled()
        configuration.buttonSize = .large
        configuration.title = "Swift"
        configuration.image = UIImage(systemName: "swift")
        configuration.imagePlacement = .trailing
        configuration.titlePadding = 10
        configuration.imagePadding = 10
        configuration.contentInsets = NSDirectionalEdgeInsets(top: 10, leading: 10, bottom: 10, trailing: 10)

        let tempButton = UIButton(configuration: configuration)
        tempButton.addAction(UIAction(handler: { action in
            print("new touched")
        }), for: .touchUpInside)

        self.view.addSubview(tempButton)

Replies

For those struggling with this; the ONLY think I could do to get this to work was add this line of code:

tempButton.translatesAutoresizingMaskIntoConstraints = false

Given the code here, the general problem is likely that you didn't size the button. At construction time it will be of size 0x0, and since UIView's don't clip by default the contents of the button will spill out, but won't be interactive. If setting translatesAutoresizingMaskIntoConstraints = false resolved the issue, its because it causes the layout system to use the buttons intrinsicContentSize to size it, but without additional constraints the position will be undefined. If you prefer to use manual layout, you should call sizeThatFits: to determine the size that the button would prefer for a given size (it can prefer a size larger than the size you pass in).