7

I want a flat looking white bordered around my UIButton. I would like it in Storyboard or programmatically. My code isn't working.

Code:

UIButton.layer.cornerRadius = 2;
UIButton.layer.borderWidth = 1;
UIButton.layer.borderColor = UIColor.whiteColor()
Cesare
  • 9,139
  • 16
  • 78
  • 130
Palash Sharma
  • 662
  • 1
  • 10
  • 18

6 Answers6

16

You should create referencing outlet for your button from storyboard to your VC named for example myButton than set its properties

myButton.layer.cornerRadius = 2;
myButton.layer.borderWidth = 1;
myButton.layer.borderColor = UIColor.whiteColor().CGColor
Zell B.
  • 10,266
  • 3
  • 40
  • 49
5

You don't have to do this with code either. You can create a stretchable image and set it to the background image of the button in the attributes inspector.

enter image description here

iOSAaronDavid
  • 140
  • 15
1

as 0x7fffffff said. UIButton is the class it can be instatiated bu invoking its constructor like this

let instanceOfUIButton = UIButton()

then you can set the desired attributes:

instanceOfUIButton.layer.cornerRadius = 2;
nqvst
  • 361
  • 1
  • 3
0

In Xcode 8.2 (Swift 3) you can use "Identity Inspector Tab". Search for "Users Defined Runtime Attributes", after selecting your UIButton. There you can define these attributes:

  1. Key Path: layer.cornerRadius; 2-Type: Number, 3-Value: 2
  2. Key Path: layer.borderWidth; 2-Type: Number, 3-Value: 1
  3. Key Path: layer.borderColor; 2-Type: Color, 3-Value: "select white or another one"
Ivan Sinigaglia
  • 1,052
  • 7
  • 16
0

Another option instead of creating a reference to each button would be to create a subclass of the type UIButton. You could then set the properties in the subclass. Next you could change the class of all the buttons in the storyboard that need to have the same properties.

class MyButton: UIButton {

 override func draw(_ rect: CGRect) {
        super.draw(rect)
        layer.borderWidth = 1.0
        layer.borderColor = UIColor.White
        layer.cornerRadius = 2
    }

}
dj8620
  • 1
  • 1
0

Add this line at the top

myButton.layer.masksToBounds = true
Paul Roub
  • 36,322
  • 27
  • 84
  • 93