カテゴリー
iOS Swift

UIViewの上下のみにボーダーをつける

class GuitarString: UIView {
	
	lazy var topBorder: UIView = {
		let line = UIView()
		line.translatesAutoresizingMaskIntoConstraints = false
		return line
	}()
	
	lazy var bottomBorder: UIView = {
		let line = UIView()
		line.translatesAutoresizingMaskIntoConstraints = false
		return line
	}()
	
	var lineWidth: CGFloat = 0.5
	var lineColor: UIColor = .black
	
	init(){
		super.init(frame: .zero)
		layout()
	}
	
	private func layout() {
		
		self.addSubview(topBorder)
		
		self.backgroundColor = .lightGray
		
		topBorder.leftAnchor.constraint(equalTo: self.leftAnchor).isActive = true
		topBorder.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
		topBorder.rightAnchor.constraint(equalTo: self.rightAnchor).isActive = true
		topBorder.heightAnchor.constraint(equalToConstant: lineWidth).isActive = true
		topBorder.backgroundColor = lineColor
		
		self.addSubview(bottomBorder)
		
		bottomBorder.leftAnchor.constraint(equalTo: self.leftAnchor).isActive = true
		bottomBorder.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
		bottomBorder.rightAnchor.constraint(equalTo: self.rightAnchor).isActive = true
		bottomBorder.heightAnchor.constraint(equalToConstant: lineWidth).isActive = true
		bottomBorder.backgroundColor = lineColor
		
	}
	
	required init?(coder: NSCoder) {
		fatalError("init(coder:) has not been implemented")
	}
}