136

I just started learning iOS development, cannot find how to make simple rounded button. I find resources for old versions. Do I need to set a custom background for a button? In Android, I would just use a 9 patch, but I know iOS does not have this capability.

enter image description here

Amit Srivastava
  • 1,105
  • 9
  • 18
Gintas_
  • 4,940
  • 12
  • 44
  • 87

16 Answers16

274

Short Answer: YES

You can absolutely make a simple rounded button without the need of an additional background image or writing any code for the same. Just follow the screenshot given below, to set the runtime attributes for the button, to get the desired result.

It won't show in the Storyboard but it will work fine when you run the project.

enter image description here

Note:
The 'Key Path' layer.cornerRadius and value is 5. The value needs to be changed according to the height and width of the button. The formula for it is the height of button * 0.50. So play around the value to see the expected rounded button in the simulator or on the physical device. This procedure will look tedious when you have more than one button to be rounded in the storyboard.

Randika Vishman
  • 7,983
  • 3
  • 57
  • 80
Nishant
  • 12,529
  • 9
  • 59
  • 94
96

You can do something like this:

@IBDesignable class MyButton: UIButton
{
    override func layoutSubviews() {
        super.layoutSubviews()

        updateCornerRadius()
    }

    @IBInspectable var rounded: Bool = false {
        didSet {
            updateCornerRadius()
        }
    }

    func updateCornerRadius() {
        layer.cornerRadius = rounded ? frame.size.height / 2 : 0
    }
}

Set class to MyButton in Identity Inspector and in IB you will have rounded property:

enter image description here

ChikabuZ
  • 10,031
  • 5
  • 63
  • 86
93

To do it in the storyboard, you need to use an image for the button.

Alternatively you can do it in code:

 btn.layer.cornerRadius = 10
 btn.clipsToBounds = true
realtimez
  • 2,525
  • 2
  • 20
  • 24
39
  1. Create a Cocoa Touch class.

enter image description here

  1. Insert the code in RoundButton class.

    import UIKit
    
    @IBDesignable
    class RoundButton: UIButton {
    
        @IBInspectable var cornerRadius: CGFloat = 0{
            didSet{
            self.layer.cornerRadius = cornerRadius
            }
        }
    
        @IBInspectable var borderWidth: CGFloat = 0{
            didSet{
                self.layer.borderWidth = borderWidth
            }
        }
    
        @IBInspectable var borderColor: UIColor = UIColor.clear{
            didSet{
                self.layer.borderColor = borderColor.cgColor
            }
        }
    }
    
  2. Refer the image.

enter image description here

Anh Pham
  • 2,108
  • 9
  • 18
  • 29
anson
  • 1,436
  • 14
  • 16
  • Don't know if others ran into this, too, but as soon as I set the class to a button and give it a radius, border width and border color Xcode (9.2) throws an error that there was a rendering error. The app building succeeds but the red "!" keeps being displayed. Just saying... I switched back to the code only solution (although this was really an elegant way). – Aeger Jan 17 '18 at 11:55
14

I found the easiest way to do this, is by setting the cornerRadius to half of the height of the view.

button.layer.cornerRadius = button.bounds.size.height/2
FrankkieNL
  • 711
  • 9
  • 22
  • No, I did not. If your height and width are the same (so it would have been square), then you would get a circle. But if the width is greater than the height, the button will be correct. – FrankkieNL Mar 20 '18 at 08:55
  • 2
    You're right, I missed the image included in the question that shows what the poster was actually after matches your answer more than the accepted answer - upvote – Michael Hudson Mar 20 '18 at 10:46
9

Xcode 13 has added an option to do this by adjusting Corner Style Property (under BackgroundConfiguration).

Have to configure the background within Background Config, and then set View Background as default (nothing).

enter image description here

hoss24
  • 556
  • 9
  • 22
  • It should be noted that you need to change your button style from Default to Plain for these options to appear. – Tim Schmidt Apr 11 '23 at 12:17
4

You can connect IBOutlet of yur button from storyboard.

Then you can set corner radius of your button to make it's corner round.

for example, your outlet is myButton then,

Obj - C

 self.myButton.layer.cornerRadius = 5.0 ;

Swift

  myButton.layer.cornerRadius = 5.0

If you want exact round button then your button's width and height must be equal and cornerRadius must be equal to height or width / 2 (half of the width or height).

Ketan Parmar
  • 27,092
  • 9
  • 50
  • 75
4

As other answer have suggested to perform most of this work in code only one answer actually provided a way to view your changes in the storyboard IB Interface. My answer goes beyond that answer by allowing you to change the cornerRadius of the view, button, image, etc.

Please take a look at the following code. To use this code create a new swift file called RoundedView or whatever you would like to call it then go to your storyboard and change the class to either "RoundedView", "RoundedImageView" or "RoundedButton".

import UIKit

@IBDesignable class RoundedImage: UIImageView
{
    override func layoutSubviews() {
        super.layoutSubviews()

        updateCornerRadius()
    }

    @IBInspectable var rounded: Bool = false {
        didSet {
            updateCornerRadius()
        }
    }

    @IBInspectable var cornerRadius: CGFloat = 0.1 {
        didSet {
            updateCornerRadius()
        }
    }

    func updateCornerRadius() {
        layer.cornerRadius = rounded ? cornerRadius : 0
        layer.masksToBounds = rounded ? true : false
    }
}

@IBDesignable class RoundedView: UIView
{
    override func layoutSubviews() {
        super.layoutSubviews()

        updateCornerRadius()
    }

    @IBInspectable var rounded: Bool = false {
        didSet {
            updateCornerRadius()
        }
    }

    @IBInspectable var cornerRadius: CGFloat = 0.1 {
        didSet {
            updateCornerRadius()
        }
    }

    func updateCornerRadius() {
        layer.cornerRadius = rounded ? cornerRadius : 0
        layer.masksToBounds = rounded ? true : false
    }
}

@IBDesignable class RoundedButton: UIButton
{
    override func layoutSubviews() {
        super.layoutSubviews()

        updateCornerRadius()
    }

    @IBInspectable var rounded: Bool = false {
        didSet {
            updateCornerRadius()
        }
    }

    @IBInspectable var cornerRadius: CGFloat = 0.1 {
        didSet {
            updateCornerRadius()
        }
    }

    func updateCornerRadius() {
        layer.cornerRadius = rounded ? cornerRadius : 0
        layer.masksToBounds = rounded ? true : false
    }
}
4

Follow the screenshot below. It works when you run the simulator (won't see it on preview)

Adding UIView rounded border

Xavier Chia
  • 203
  • 3
  • 5
2

I am using Xcode Version 11.4

In the attribute inspector, you can define the corner radius.

It won't show in the Storyboard but it will work fine when you run the project.

enter image description here

casillas
  • 16,351
  • 19
  • 115
  • 215
2

change background default to custom enter image description here

then give border radius

enter image description here

result is like this

enter image description here

Rasel Khan
  • 2,976
  • 19
  • 25
1

While adding layer.cornerRadius in the storyboard make sure that you don't have leading or trailing spaces. If you do copy paste, you might get spaces inserted. Would be nice if XCode say some kind of warning or error.

Noam Hacker
  • 4,671
  • 7
  • 34
  • 55
1

Alternatively you can do in code on UIKit and iOS 15+

    var config = UIButton.Configuration.filled()
    config.cornerStyle = .capsule
    let btn = UIButton(configuration: config)
    btn.tintColor = .blue
Mr.SwiftOak
  • 1,469
  • 3
  • 8
  • 19
0

The extension is the best option for this problem. Create an extension of View or Button

public extension UIView {
  //Round the corners
  func roundCorners(){
    let radius = bounds.maxX / 16
    layer.cornerRadius = radius
  }
}

Call it from the code

button.roundCorners()
-1

Try this!!

 override func viewDidLoad() {
   super.viewDidLoad()

   var button = UIButton.buttonWithType(.Custom) as UIButton
   button.frame = CGRectMake(160, 100, 200,40)

   button.layer.cornerRadius =5.0
   button.layer.borderColor = UIColor.redColor().CGColor
   button.layer.borderWidth = 2.0

   button.setImage(UIImage(named:"Placeholder.png"), forState: .Normal)
   button.addTarget(self, action: "OnClickroundButton", forControlEvents: .TouchUpInside)
   button.clipsToBounds = true

   view.addSubview(button)
}
    func OnClickroundButton() {
   NSLog(@"roundButton Method Called");
}
Sanjeet Verma
  • 551
  • 4
  • 15
-1
import UIKit

@IBDesignable class MyButton: UIButton
{
    override func layoutSubviews() {
        super.layoutSubviews()


    }

    func updateCornerRadius(radius:CGFloat) {
        layer.cornerRadius = radius
    }
    @IBInspectable var cornerRadius:CGFloat = 0{
        didSet{
            updateCornerRadius(radius: cornerRadius)
        }
    }
}
Rodrigo Salomao
  • 143
  • 1
  • 10