Skip to content

Instantly share code, notes, and snippets.

@ftp27
Created June 27, 2019 15:03
Show Gist options
  • Save ftp27/c53e423bfc34e6baf22eee574a546f59 to your computer and use it in GitHub Desktop.
Save ftp27/c53e423bfc34e6baf22eee574a546f59 to your computer and use it in GitHub Desktop.
AutoLayout Hugging Priority and Compression Resistance Priority tests
import PlaygroundSupport
import UIKit
func makeLabels(left: String, right: String) -> (UIView, UILabel, UILabel) {
let leftLb: UILabel = {
let label = UILabel(frame: .zero)
label.textAlignment = .left
label.backgroundColor = .blue
label.text = left
return label
}()
let rightLb: UILabel = {
let label = UILabel(frame: .zero)
label.textAlignment = .right
label.backgroundColor = .purple
label.text = right
return label
}()
let view = UIView(frame: .zero)
for v: UILabel in [leftLb, rightLb] {
v.translatesAutoresizingMaskIntoConstraints = false
v.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.regular)
v.numberOfLines = 0
v.textColor = .white
view.addSubview(v)
}
NSLayoutConstraint.activate([
leftLb.leadingAnchor.constraint(equalTo: view.leadingAnchor),
leftLb.topAnchor.constraint(equalTo: view.topAnchor),
leftLb.bottomAnchor.constraint(equalTo: view.bottomAnchor),
rightLb.leadingAnchor.constraint(equalTo: leftLb.trailingAnchor),
rightLb.topAnchor.constraint(equalTo: view.topAnchor),
rightLb.trailingAnchor.constraint(equalTo: view.trailingAnchor),
rightLb.bottomAnchor.constraint(equalTo: view.bottomAnchor),
])
return (view, leftLb, rightLb)
}
let frame = CGRect(x:0, y:0, width: 300, height: 250)
let contentView = UIView(frame: frame)
contentView.backgroundColor = .white
let stack = UIStackView(frame: frame)
stack.axis = .vertical
stack.distribution = .fillEqually
stack.alignment = .fill
stack.spacing = 2
stack.translatesAutoresizingMaskIntoConstraints = false
contentView.addSubview(stack)
NSLayoutConstraint.activate([
stack.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 5),
stack.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 5),
stack.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -5),
stack.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -5),
])
stack.addArrangedSubview({
let (view,left,right) = makeLabels(left: "Hugging High", right: "Hugging High")
left.setContentHuggingPriority(.defaultHigh, for: .horizontal)
right.setContentHuggingPriority(.defaultHigh, for: .horizontal)
return view
}())
stack.addArrangedSubview({
let (view,left,right) = makeLabels(left: "Hugging Low", right: "Hugging High")
left.setContentHuggingPriority(.defaultLow, for: .horizontal)
right.setContentHuggingPriority(.defaultHigh, for: .horizontal)
return view
}())
stack.addArrangedSubview({
let (view,left,right) = makeLabels(left: "Compression High", right: "Compression High")
left.setContentCompressionResistancePriority(.defaultHigh, for: .horizontal)
right.setContentCompressionResistancePriority(.defaultHigh, for: .horizontal)
return view
}())
stack.addArrangedSubview({
let (view,left,right) = makeLabels(left: "Compression High", right: "Compression Low")
left.setContentCompressionResistancePriority(.defaultHigh, for: .horizontal)
right.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
return view
}())
stack.addArrangedSubview({
let (view,left,right) = makeLabels(left: "Hugging Low\nCompression High", right: "Hugging High\nCompression Low")
left.setContentHuggingPriority(.defaultLow, for: .horizontal)
right.setContentHuggingPriority(.defaultHigh, for: .horizontal)
left.setContentCompressionResistancePriority(.defaultHigh, for: .horizontal)
right.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
return view
}())
stack.addArrangedSubview({
let (view,left,right) = makeLabels(left: "Hugging Low\nCompression Low", right: "Hugging High\nCompression Hight")
left.setContentHuggingPriority(.defaultLow, for: .horizontal)
right.setContentHuggingPriority(.defaultHigh, for: .horizontal)
left.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
right.setContentCompressionResistancePriority(.defaultHigh, for: .horizontal)
return view
}())
PlaygroundPage.current.liveView = contentView
@ftp27
Copy link
Author

ftp27 commented Jun 27, 2019

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment