Skip to content

Instantly share code, notes, and snippets.

@halgatewood
Last active January 15, 2020 16:54
Show Gist options
  • Save halgatewood/c6927673126bb702d414dcfd0b4cf134 to your computer and use it in GitHub Desktop.
Save halgatewood/c6927673126bb702d414dcfd0b4cf134 to your computer and use it in GitHub Desktop.
[**Requires LBTATools**] A sticky function to stick stuff using Swift to other sticky stuff. Padding approached from a web developers perspective.
// USAGES:
// SQUARE WITH A TOP PADDING OF 20
icon.stick(top: view.topAnchor, padTop: 20, width: 80, height: 80)
// PADDING TOP AND RIGHT
label.stick(top: view.topAnchor, trailing: view.trailingAnchor, padding: [20,20,0,0] )
// PADDING TOP AND BOTTOM = 20, RIGHT AND LEFT = 0
label.stick(top: view.topAnchor, trailing: view.trailingAnchor, padding: [20,0] )
// PADDING 20 ALL AROUND
label.stick(top: view.topAnchor, trailing: view.trailingAnchor, padding: [20] )
// FUNCTION:
@discardableResult
open func stick(top: NSLayoutYAxisAnchor?, leading: NSLayoutXAxisAnchor? = nil, bottom: NSLayoutYAxisAnchor? = nil, trailing: NSLayoutXAxisAnchor? = nil, padTop: CGFloat = 0, padRight: CGFloat = 0, padBottom: CGFloat = 0, padLeft: CGFloat = 0, padding: [CGFloat] = [0,0,0,0], width: CGFloat = 0, height: CGFloat = 0) -> AnchoredConstraints
{
translatesAutoresizingMaskIntoConstraints = false
var anchoredConstraints = AnchoredConstraints()
var paddingTop = padTop
var paddingRight = padRight
var paddingBottom = padBottom
var paddingLeft = padLeft
if padding.count == 2
{
paddingTop = padding[0]
paddingBottom = padding[0]
paddingRight = padding[1]
paddingLeft = padding[1]
}
else if padding.count == 1
{
paddingTop = padding[0]
paddingBottom = padding[0]
paddingRight = padding[0]
paddingLeft = padding[0]
}
else
{
if ( padding.count > 0 && padding[0] != 0 )
{
paddingTop = padding[0]
}
if ( padding.count > 1 && padding[1] != 0 )
{
paddingRight = padding[1]
}
if ( padding.count > 2 && padding[2] != 0 )
{
paddingBottom = padding[2]
}
if ( padding.count > 3 && padding[3] != 0 )
{
paddingLeft = padding[3]
}
}
if let top = top {
anchoredConstraints.top = topAnchor.constraint(equalTo: top, constant: paddingTop)
}
if let leading = leading {
anchoredConstraints.leading = leadingAnchor.constraint(equalTo: leading, constant: paddingLeft)
}
if let bottom = bottom {
anchoredConstraints.bottom = bottomAnchor.constraint(equalTo: bottom, constant: -paddingBottom)
}
if let trailing = trailing {
anchoredConstraints.trailing = trailingAnchor.constraint(equalTo: trailing, constant: -paddingRight)
}
if width != 0 {
anchoredConstraints.width = widthAnchor.constraint(equalToConstant: width)
}
if height != 0 {
anchoredConstraints.height = heightAnchor.constraint(equalToConstant: height)
}
[anchoredConstraints.top, anchoredConstraints.leading, anchoredConstraints.bottom, anchoredConstraints.trailing, anchoredConstraints.width, anchoredConstraints.height].forEach{ $0?.isActive = true }
return anchoredConstraints
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment