I wanted to use tuples like so:
myTopAnchor = myButton.layoutAnchors(
top: (view.topAnchor, 0),
// left: (nil, 0),
// bottom: (nil, 0),
right: (view.rightAnchor, 0),
width: 60, height: 50).first // notice subscript
So, I rejiggered one of your layout convenience methods like this:
// anchor convenience method
typealias X_ANCHOR = (anchor: NSLayoutXAxisAnchor?, k: Int)
typealias Y_ANCHOR = (anchor: NSLayoutYAxisAnchor?, k: Int)
// anchor convenience method
// all parameters are optional
func layoutAnchors
(top : Y_ANCHOR = (nil, 0),
left : X_ANCHOR = (nil, 0),
bottom : Y_ANCHOR = (nil, 0),
right : X_ANCHOR = (nil, 0),
width : Int = 0,
height : Int = 0) -> [NSLayoutConstraint] {
translatesAutoresizingMaskIntoConstraints = false
var anchors = [NSLayoutConstraint]()
if top.0 != nil {
anchors.append(topAnchor.constraint(equalTo: top.0!,
constant: CGFloat(top.1)))
}
if left.0 != nil {
anchors.append(leftAnchor.constraint(equalTo: left.0!,
constant: CGFloat(left.1)))
}
if bottom.0 != nil {
anchors.append(bottomAnchor.constraint(equalTo: bottom.0!,
constant: CGFloat(-bottom.1)))
}
if right.0 != nil {
anchors.append(rightAnchor.constraint(equalTo: right.0!,
constant: CGFloat(-right.1)))
}
if width > 0 {
anchors.append(widthAnchor.constraint(equalToConstant: CGFloat(width)))
}
if height > 0 {
anchors.append(heightAnchor.constraint(equalToConstant: CGFloat(height)))
}
anchors.forEach {$0.isActive = true} // activate each one
return anchors
} // end
Trouble is the autocomplete doesn't look right: It only shows the first of each tuple. Also, is the $0 type-checked I wonder...