Created
January 10, 2018 09:04
-
-
Save takuoka/1461441cf6a5329b512eac0dbba739e0 to your computer and use it in GitHub Desktop.
Add border line view to UIView by AutoLayout or FrameLayout
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// UIView+border.swift | |
// Makuake | |
// | |
// Created by Takuya Okamoto on 1/9/18. | |
// Copyright © 2018 CyberAgent Crowd Funding, Inc. All rights reserved. | |
// | |
import UIKit | |
extension UIView { | |
enum BorderPosition { | |
case top | |
case right | |
case bottom | |
case left | |
} | |
enum LayoutType { | |
case autoLayout | |
case frameLayout | |
} | |
func addBorderLineViews(positions: [BorderPosition], borderWidth: CGFloat, borderColor: UIColor? = .appBorder, with layoutType: LayoutType) { | |
positions.forEach { position in | |
let line = UIView() | |
line.backgroundColor = borderColor | |
addSubview(line) | |
switch layoutType { | |
case .autoLayout: | |
setAutoLayout(line: line, position: position, borderWidth: borderWidth) | |
case .frameLayout: | |
line.frame = borderFrame(position: position, borderWidth: borderWidth) | |
} | |
} | |
} | |
private func borderFrame(position: BorderPosition, borderWidth: CGFloat) -> CGRect { | |
switch position { | |
case .top : return CGRect(x: 0.0, y: 0.0, width: frame.width, height: borderWidth) | |
case .left : return CGRect(x: 0.0, y: 0.0, width: borderWidth, height: frame.height) | |
case .right : return CGRect(x: frame.width - borderWidth, y: 0.0, width: borderWidth, height: frame.height) | |
case .bottom: return CGRect(x: 0.0, y: frame.height - borderWidth, width: frame.width, height: borderWidth) | |
} | |
} | |
private func setAutoLayout(line: UIView, position: BorderPosition, borderWidth: CGFloat) { | |
line.translatesAutoresizingMaskIntoConstraints = false | |
switch position { | |
case .top: | |
line.topAnchor.constraint(equalTo: topAnchor).isActive = true | |
line.leftAnchor.constraint(equalTo: leftAnchor).isActive = true | |
line.rightAnchor.constraint(equalTo: rightAnchor).isActive = true | |
line.heightAnchor.constraint(equalToConstant: borderWidth).isActive = true | |
case .left: | |
line.leftAnchor.constraint(equalTo: leftAnchor).isActive = true | |
line.topAnchor.constraint(equalTo: topAnchor).isActive = true | |
line.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true | |
line.widthAnchor.constraint(equalToConstant: borderWidth).isActive = true | |
case .right: | |
line.rightAnchor.constraint(equalTo: rightAnchor).isActive = true | |
line.topAnchor.constraint(equalTo: topAnchor).isActive = true | |
line.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true | |
line.widthAnchor.constraint(equalToConstant: borderWidth).isActive = true | |
case .bottom: | |
line.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true | |
line.leftAnchor.constraint(equalTo: leftAnchor).isActive = true | |
line.rightAnchor.constraint(equalTo: rightAnchor).isActive = true | |
line.heightAnchor.constraint(equalToConstant: borderWidth).isActive = true | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment