Skip to content

Instantly share code, notes, and snippets.

@SlaunchaMan
Created June 21, 2016 03:05
Show Gist options
  • Save SlaunchaMan/53f5b559532a24f6ee5e12968e105c27 to your computer and use it in GitHub Desktop.
Save SlaunchaMan/53f5b559532a24f6ee5e12968e105c27 to your computer and use it in GitHub Desktop.
Pinning a UIView to the Home button side of its superview in Swift 3
import UIKit
extension UIView {
public func constraintsForPinningToHomeButton(withSpacing spacing: CGFloat = 0.0) -> [NSLayoutConstraint] {
guard let superview = superview else { return [] }
switch (UIDevice.current().orientation, UIApplication.shared().statusBarOrientation) {
case (.landscapeLeft, .landscapeRight):
let centerConstraint = centerYAnchor.constraint(equalTo: superview.centerYAnchor)
centerConstraint.isActive = true
centerConstraint.identifier = "View centered vertically in superview"
let rightConstraint = rightAnchor.constraint(equalTo: superview.rightAnchor, constant: -spacing)
rightConstraint.isActive = true
rightConstraint.identifier = "View anchored to right side of superview"
return [centerConstraint, rightConstraint]
case (.landscapeRight, .landscapeLeft):
let centerConstraint = centerYAnchor.constraint(equalTo: superview.centerYAnchor)
centerConstraint.isActive = true
centerConstraint.identifier = "Vuew centered vertically in superview"
let leftConstraint = leftAnchor.constraint(equalTo: superview.leftAnchor, constant: spacing)
leftConstraint.isActive = true
leftConstraint.identifier = "Take Picture button anchored to right side of view"
return [centerConstraint, leftConstraint]
default:
let bottomConstraint = bottomAnchor.constraint(equalTo: superview.bottomAnchor, constant: -20)
bottomConstraint.isActive = true
bottomConstraint.identifier = "View anchored to bottom of superview"
let centerConstraint = centerXAnchor.constraint(equalTo: superview.centerXAnchor)
centerConstraint.isActive = true
centerConstraint.identifier = "View centered horizontally in superview"
return [bottomConstraint, centerConstraint]
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment