Is there an easy way to add a border to a view in Xcode for iOS 5.1

I want to have a border around a UIView . I just want to visually separate it from the main view.

I looked at the settings for UIView in the storyboard editor, but I can not find anything to set the border.

Is there an easy way to do this in code?

+45
ios border uiview
Jun 18 '12 at 11:30
source share
2 answers

With Swift and Xcode 6 you can do it.

Click the UIView element in the Storyboard and go to the identity inspector. In custom runtime attributes, type:

 layer.borderWidth number 1 

If you need beautiful angles

 layer.cornerRadius number 5 layer.masksToBounds boolean true 

Now this will give you a border, but to set the color you need to do this with code. Go to your view controller and add an IBOutlet from your UIView. Say what you do

 @IBOutlet weak var xView: UIView! 

Call this in the viewDidLoad function as shown below to set the color.

 xView.layer.borderColor = UIColor.whiteColor().CGColor 

Thank!

+22
Feb 01 '15 at 4:46
source share

With this border, the background still appears. In other words, the border is not projected onto the exterior, but onto the interior.

I think itโ€™s necessary to create an invoice view behind the owner with a size (width x height) enlarged with the size of the border.

+1
Aug 24 '13 at 1:34 on
source share



All Articles