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

IosUiviewBorder

Ios Problem Overview


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

I have looked in the settings for the UIView in storyboard editor but I can't seem to find anything for setting a border.

Is there an easy way to do this in code?

Ios Solutions


Solution 1 - Ios

With Swift and XCode 6 you can do this.

Click the UIView element in Storyboard, and go to identity inspector. In the user defined runtime attributes, enter:

layer.borderWidth number 1

If you want nice looking corners

layer.cornerRadius number 5
layer.masksToBounds boolean true

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

@IBOutlet weak var xView: UIView!

Call this in the viewDidLoad function like below to set the colour.

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

Thanks!

Solution 2 - Ios

With this border, background still appears behind. In other words, the border isn't projected to outer of view, but to inner space.

I think that is necessary create a overlay view behind the owner with size (width x height) increased with border size.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser1282180View Question on Stackoverflow
Solution 1 - IosmtiszView Answer on Stackoverflow
Solution 2 - IosseufagnerView Answer on Stackoverflow