Add HairlineView
jvisenti opened this issue · 3 comments
jvisenti commented
final class HairlineView: UIView {
var axis: UILayoutConstraintAxis {
didSet {
invalidateIntrinsicContentSize()
setNeedsUpdateConstraints()
}
}
var thickness: CGFloat {
didSet {
invalidateIntrinsicContentSize()
setNeedsUpdateConstraints()
}
}
private var thicknessConstraint: NSLayoutConstraint?
init(axis: UILayoutConstraintAxis, thickness: CGFloat = CGFloat(1.0 / UIScreen.mainScreen().scale), color: UIColor? = UIColor(named: .Black).colorWithAlphaComponent(0.3)) {
self.axis = axis
self.thickness = thickness
super.init(frame: .zero)
self.backgroundColor = color
setNeedsUpdateConstraints()
}
@available(*, unavailable) required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func updateConstraints() {
thicknessConstraint?.active = false
thicknessConstraint = ((axis == .Horizontal ? heightAnchor : widthAnchor) == thickness)
super.updateConstraints()
}
override func intrinsicContentSize() -> CGSize {
var size = CGSize(width: UIViewNoIntrinsicMetric, height: UIViewNoIntrinsicMetric)
switch axis {
case .Horizontal:
size.height = thickness
case .Vertical:
size.width = thickness
}
return size
}
override func contentHuggingPriorityForAxis(axis: UILayoutConstraintAxis) -> UILayoutPriority {
return (self.axis == axis ? UILayoutPriorityRequired : UILayoutPriorityDefaultLow)
}
override func contentCompressionResistancePriorityForAxis(axis: UILayoutConstraintAxis) -> UILayoutPriority {
return contentHuggingPriorityForAxis(axis)
}
}
KingOfBrian commented
Just a note that this will require a dependency on Anchorage
jvisenti commented
@KingOfBrian Only for 1 line. We should just update this to use a regular NSConstraint init
KingOfBrian commented
Yea, probably makes sense here.
I have some code that depends on Anchorage too. It may be inevitable that we want to include it eventually if we want to include custom views in the repository.