how to create uibutton with corner radius?

Generated on 8/8/2024

1 search

To create a UIButton with a corner radius, you can use the layer property of the UIButton to set the cornerRadius. Here's a simple example in Swift:

let button = UIButton(type: .system)
button.setTitle("My Button", for: .normal)
button.backgroundColor = .systemBlue
button.setTitleColor(.white, for: .normal)
button.frame = CGRect(x: 50, y: 50, width: 200, height: 50)

// Set the corner radius
button.layer.cornerRadius = 10
button.clipsToBounds = true

In this example, the cornerRadius property is set to 10, which will round the corners of the button. The clipsToBounds property is set to true to ensure that the subviews are confined to the bounds of the button, which is necessary for the corner radius to be visible.

For more advanced button customization, you might want to look into sessions like Create custom hover effects in visionOS which discusses various effects and customizations you can apply to buttons and other UI elements.