Skip to content

Instantly share code, notes, and snippets.

@ArchieGoodwin
Created October 23, 2017 09:14
Show Gist options
  • Save ArchieGoodwin/e0ece7770eeb6c747cde9b4b281186ce to your computer and use it in GitHub Desktop.
Save ArchieGoodwin/e0ece7770eeb6c747cde9b4b281186ce to your computer and use it in GitHub Desktop.
Add border to any side of UIView
import Foundation
extension UIView{
func border(color : UIColor, side : String, width : CGFloat)
{
var borderFrame = CGRect.zero
switch side {
case "left":
borderFrame = CGRect(x: 0.0, y: 0.0, width: width, height: self.frame.size.height)
break
case "right":
borderFrame = CGRect(x: self.frame.size.width - 1.0, y: 0.0, width: width, height: self.frame.size.height)
break
case "top":
borderFrame = CGRect(x: 0.0, y: 0.0, width: self.frame.size.width, height: width)
break
case "bottom":
borderFrame = CGRect(x: 0.0, y: self.frame.size.height - 1, width: self.frame.size.width, height: width)
break
default:
borderFrame = CGRect(x: 0.0, y: 0.0, width: width, height: self.frame.size.height)
break
}
let border : CALayer = CALayer()
border.backgroundColor = color.cgColor
border.frame = borderFrame
border.name = side
self.layer.addSublayer(border)
}
func removeBorderForName(name : String)
{
if let lrs = self.layer.sublayers, lrs.count > 0
{
for lr in self.layer.sublayers!
{
if lr.name == name
{
lr.removeFromSuperlayer()
break
}
}
}
}
func border(color : UIColor, side : String, width : CGFloat, shift : CGFloat)
{
var borderFrame = CGRect.zero
switch side {
case "left":
borderFrame = CGRect(x: shift, y: 0.0, width: width, height: self.frame.size.height)
break
case "right":
borderFrame = CGRect(x: self.frame.size.width - 1.0 + shift, y: 0.0, width: width, height: self.frame.size.height)
break
case "top":
borderFrame = CGRect(x: 0.0, y: shift, width: self.frame.size.width, height: width)
break
case "bottom":
borderFrame = CGRect(x: 0.0, y: self.frame.size.height - 1 + shift, width: self.frame.size.width, height: width)
break
default:
borderFrame = CGRect(x: 0.0, y: 0.0, width: width, height: self.frame.size.height)
break
}
let border : CALayer = CALayer()
border.backgroundColor = color.cgColor
border.frame = borderFrame
border.name = side
self.layer.addSublayer(border)
}
func border(color : UIColor, frameForBorder : CGRect)
{
let border : CALayer = CALayer()
border.backgroundColor = color.cgColor
border.frame = frameForBorder
self.layer.addSublayer(border)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment