view에 border 추가

# 전체 border

view.layer.borderColor = UIColor.red.cgColor
view.layer.borderWidth = 1.0

 

# 특정 부분만 border

extension CALayer {

func addBorder(_ arr_edge: [UIRectEdge], color: UIColor, width: CGFloat) {

for edge in arr_edge {

let border = CALayer()

switch edge {

case UIRectEdge.top:

border.frame = CGRect.init(x: 0, y: 0, width: frame.width, height: width)

break

case UIRectEdge.bottom:

border.frame = CGRect.init(x: 0, y: frame.height – width, width: frame.width, height: width)

break

case UIRectEdge.left:

border.frame = CGRect.init(x: 0, y: 0, width: width, height: frame.height)

break

case UIRectEdge.right:

border.frame = CGRect.init(x: frame.width – width, y: 0, width: width, height: frame.height)

break

default:

break

}

border.backgroundColor = color.cgColor;

self.addSublayer(border)

}

}

}

 

view.layer.addBorder([.top, .bottom], color: UIColor.white, width: 1.0)

카테고리 iOS

답글 남기기