Bordered iOS 8 Buttons Swift

I want white white to be edged around my UIButton . I would like it in the Storyboard or programmatically. My code is not working.

Code:

 UIButton.layer.cornerRadius = 2; UIButton.layer.borderWidth = 1; UIButton.layer.borderColor = UIColor.whiteColor() 
+6
source share
6 answers

You must create a link for your button from the storyboard to your VC, for example, myButton, than set its properties

 myButton.layer.cornerRadius = 2; myButton.layer.borderWidth = 1; myButton.layer.borderColor = UIColor.whiteColor().CGColor 
+16
source

You also do not need to do this with code. You can create a stretchable image and set it to the background image of the button in the attribute inspector.

enter image description here

+5
source

as said 0x7fffffff. UIButton is a class that can be set to bu calling its constructor like this

 let instanceOfUIButton = UIButton() 

then you can set the desired attributes:

 instanceOfUIButton.layer.cornerRadius = 2; 
+1
source

In Xcode 8.2 (Swift 3), you can use the "Inspector Inspector" tab. Locate the "User Defined Runtime Attributes" after selecting your UIButton. There you can define these attributes:

  • Key path: layer.cornerRadius; 2-Type: Number, 3-Value: 2
  • Key path: layer.borderWidth; 2-Type: Number, 3-Value: 1
  • Key path: layer.borderColor; 2-Type: Color, 3-Value: "choose white or another"
0
source

Another option, instead of creating a link to each button, is to create a subclass of type UIButton. Then you can set the properties in a subclass. Then you can change the class of all buttons in the storyboard that should 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 } } 
0
source

Add this line at the top

 myButton.layer.masksToBounds = true 
0
source

Source: https://habr.com/ru/post/981046/


All Articles