My question is on the title.
I don't know how to add a border in a specific side, top or bottom, any side...
layer.border
draws the border for the whole view...
My question is on the title.
I don't know how to add a border in a specific side, top or bottom, any side...
layer.border
draws the border for the whole view...
In Swift 4 and 3
Building off of NSBum's answer, I took a similar approach and created this simple UIView subclass so that it works in Interface Builder and works with constraints: github link
By using CGContextFillRect instead of CGContextStrokePath, I was able to predictably keep the lines completely solid and within the bounds of the view.
Here's my blog post about it: http://natrosoft.com/?p=55
-- Basically just drop in a UIView in Interface Builder and change its class type to NAUIViewWithBorders.
-- Then in your VC's viewDidLoad do something like:
Here's a direct link to the .m file so you can see the implementation. There is a demo project as well. Hope this helps someone :)
Just posting here to help someone looking for adding borders. I have made a few changes in the accepted answer here swift label only border left. Changed width in case
UIRectEdge.Top
fromCGRectGetHeight(self.frame)
toCGRectGetWidth(self.frame)
and in caseUIRectEdge.Bottom
fromUIScreen.mainScreen().bounds.width
toCGRectGetWidth(self.frame)
to get borders correctly. Using Swift 2.Finally the extension is :
Here's a Swift 4 version of Pauls's answer
Swift version:
Edit: For updated versions check my repo here: https://github.com/goktugyil/EZSwiftExtensions/blob/master/Sources/UIViewExtensions.swift
Look at the addBorder parts
Swift 3.0
Swift 4.1