Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import PlaygroundSupport
import UIKit
extension NSLayoutConstraint {
class func equalConstraint(item: UIView, attribute: NSLayoutAttribute, toItem: UIView) -> NSLayoutConstraint{
return NSLayoutConstraint(item: item,
attribute: attribute,
relatedBy: .equal,
toItem: toItem,
attribute: attribute,
multiplier: 1.0,
constant: 0.0)
}
class func constraints(from attributes: [NSLayoutAttribute], item: UIView, toItem:UIView) -> [NSLayoutConstraint] {
return attributes.map(){
equalConstraint(item: item,
attribute: $0,
toItem: toItem)
}
}
class func rectCostraints(item: UIView, toItem: UIView) -> [NSLayoutConstraint] {
return constraints(from: [.top, .bottom, .trailing, .leading],
item: item,
toItem: toItem)
}
}
class CircleView: UIView {
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
func commonInit() {
layer.masksToBounds = true
}
override func layoutSubviews() {
layer.cornerRadius = layer.bounds.width/2
}
}
let frame = CGRect(x:0, y:0, width: 300, height:300)
let view = UIView(frame: frame)
view.backgroundColor = UIColor.white
let stack = UIStackView(frame: frame)
stack.axis = .horizontal
stack.alignment = .fill
stack.distribution = .fillEqually
stack.spacing = 8.0
stack.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(stack)
view.addConstraints(
NSLayoutConstraint.constraints(from: [.trailing, .leading, .centerY],
item: stack,
toItem: view))
for index in 1...5 {
if index % 2 == 0 {
print("-", terminator:" ")
let containerView = UIView(frame: CGRect(x:0, y:0, width: 40, height:20))
let line = UIView(frame: CGRect(x:0, y:0, width: 40, height:2))
line.backgroundColor = UIColor.gray
containerView.addSubview(line)
line.translatesAutoresizingMaskIntoConstraints = false
containerView.addConstraints(
NSLayoutConstraint.constraints(from: [.trailing, .leading, .centerY],
item: line,
toItem: containerView))
line.addConstraint(NSLayoutConstraint(item: line,
attribute: .height,
relatedBy: .equal,
toItem: nil ,
attribute: .notAnAttribute,
multiplier: 1.0,
constant: 2.0))
stack.addArrangedSubview(containerView)
} else {
print(String(index/2 + 1), terminator:" ")
let containerView = CircleView(frame: CGRect(x:0, y:0, width: 20, height:20))
containerView.backgroundColor = index == 1 ? UIColor.blue : UIColor.gray
let label = UILabel()
label.text = String(index/2 + 1)
label.textAlignment = .center
label.textColor = UIColor.white
label.translatesAutoresizingMaskIntoConstraints = false
containerView.addSubview(label)
containerView.addConstraints(
NSLayoutConstraint.rectCostraints(item: label, toItem: containerView).map(){ consraint in
switch consraint.firstAttribute {
case .leading, .top:
consraint.constant = 2.0
default:
consraint.constant = -2.0
}
return consraint
}
)
containerView.addConstraint(NSLayoutConstraint(item: containerView, attribute: .width,
relatedBy: .equal,
toItem: containerView,
attribute: .height,
multiplier: 1.0,
constant: 0.0))
stack.addArrangedSubview(containerView)
}
}
PlaygroundPage.current.liveView = view
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment