Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a border inside a uiview?

I am having a uiview and I would like to add a border iside this UIVIew approximately 75 % of UIView. Could any one help with this. I could get the solution to draw the border outside.

like image 220
Ashwin P Avatar asked Mar 03 '15 05:03

Ashwin P


People also ask

What is layer Swift?

Overview. Layers are often used to provide the backing store for views but can also be used without a view to display content. A layer's main job is to manage the visual content that you provide but the layer itself has visual attributes that can be set, such as a background color, border, and shadow.


2 Answers

Well there isn't simply a little property you can set to align the border to the outside. It draws aligned to the inside because the UIViews default drawing operations draw within its bounds.

The simplest solution that comes to mind would be to expand the UIView by the size of the border width when applying the border:

CGFloat borderWidth = 2.0f;

self.frame = CGRectInset(self.frame, -borderWidth, -borderWidth);
self.layer.borderColor = [UIColor yellowColor].CGColor;
self.layer.borderWidth = borderWidth;
like image 181
aroragourav Avatar answered Oct 13 '22 06:10

aroragourav


@aroragourav's answer for Swift 3+

let borderWidth: CGFloat = 2

frame = frame.insetBy(dx: -borderWidth, dy: -borderWidth)
layer.borderColor = UIColor.yellow.cgColor
layer.borderWidth = borderWidth
like image 7
James Zaghini Avatar answered Oct 13 '22 07:10

James Zaghini