Last active
June 19, 2019 00:40
-
-
Save jimmyhoran/80374a835c97f1c3155a72932d46e059 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
import PlaygroundSupport | |
extension UIView { | |
/// Anchor self to `view` center x and y axis. | |
/// | |
/// - parameters: | |
/// - centerX: Center align to the view along the x axis. | |
/// - centerY: Center align to the view along the y axis. | |
/// - view: View to apply the relevant constraints. | |
/// - shouldRespectSafeAreaLayoutGuide: If true the constraints will | |
/// respect the safe area layout guides. | |
public func anchor( | |
centerX: Bool = false, | |
centerY: Bool = false, | |
to view: UIView, | |
shouldRespectSafeAreaLayoutGuide: Bool = false) { | |
// Disabling the `translatesAutoresizingMaskIntoConstraints` property | |
// to use Auto Layout constraints | |
translatesAutoresizingMaskIntoConstraints = false | |
// Center constraints | |
if #available(iOS 11, *), shouldRespectSafeAreaLayoutGuide { | |
centerXAnchor.constraint(equalTo: view.safeAreaLayoutGuide.centerXAnchor).isActive = centerX | |
centerYAnchor.constraint(equalTo: view.safeAreaLayoutGuide.centerYAnchor).isActive = centerY | |
} else { | |
centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = centerX | |
centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = centerY | |
} | |
} | |
/// Constrain width and height to another view. | |
/// | |
/// - parameters: | |
/// - view: View to apply the relevant constraints. | |
/// - shouldRespectSafeAreaLayoutGuide: If true the constraints will | |
/// respect the safe area layout guides. | |
public func anchorSize( | |
to view: UIView, | |
shouldRespectSafeAreaLayoutGuide: Bool = false) { | |
// Disabling the `translatesAutoresizingMaskIntoConstraints` property | |
// to use Auto Layout constraints | |
translatesAutoresizingMaskIntoConstraints = false | |
// Size constraints | |
if #available(iOS 11, *), shouldRespectSafeAreaLayoutGuide { | |
widthAnchor.constraint(equalTo: view.safeAreaLayoutGuide.widthAnchor).isActive = true | |
heightAnchor.constraint(equalTo: view.safeAreaLayoutGuide.heightAnchor).isActive = true | |
} else { | |
widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true | |
heightAnchor.constraint(equalTo: view.heightAnchor).isActive = true | |
} | |
} | |
/// Anchor the view to various constraints. | |
/// | |
/// - parameters: | |
/// - top: Align the views top to `NSLayoutYAxisAnchor` | |
/// - bottom: Align the views bottom to `NSLayoutYAxisAnchor` | |
/// - leading: Align the views leading to `NSLayoutXAxisAnchor` | |
/// - trailing: Align the views trailing to `NSLayoutXAxisAnchor` | |
/// - padding: Padding constraints | |
/// - size: Size constraints | |
public func anchor( | |
top: NSLayoutYAxisAnchor? = nil, | |
bottom: NSLayoutYAxisAnchor? = nil, | |
leading: NSLayoutXAxisAnchor? = nil, | |
trailing: NSLayoutXAxisAnchor? = nil, | |
padding: UIEdgeInsets, | |
size: CGSize = .zero) { | |
// Disabling the `translatesAutoresizingMaskIntoConstraints` property | |
// to use Auto Layout constraints | |
translatesAutoresizingMaskIntoConstraints = false | |
// Top constraint | |
if let top = top { | |
topAnchor.constraint( | |
equalTo: top, | |
constant: padding.top).isActive = true | |
} | |
// Bottom constraint | |
if let bottom = bottom { | |
bottomAnchor.constraint( | |
equalTo: bottom, | |
constant: -padding.bottom).isActive = true | |
} | |
// Leading constraint | |
if let leading = leading { | |
leadingAnchor.constraint( | |
equalTo: leading, | |
constant: padding.left).isActive = true | |
} | |
// Trailing constraint | |
if let trailing = trailing { | |
trailingAnchor.constraint( | |
equalTo: trailing, | |
constant: -padding.right).isActive = true | |
} | |
// Each size constraint should only be set if NOT zero | |
if size.width != 0 { | |
widthAnchor.constraint(equalToConstant: size.width).isActive = true | |
} | |
if size.height != 0 { | |
heightAnchor.constraint(equalToConstant: size.height).isActive = true | |
} | |
} | |
} | |
class MyViewController: UIViewController { | |
override func loadView() { | |
let view = UIView() | |
view.backgroundColor = .white | |
let greenBox = UIView() | |
greenBox.backgroundColor = .green | |
let redBox = UIView() | |
redBox.backgroundColor = .red | |
// Add all the views | |
[greenBox, redBox].forEach { view.addSubview($0) } | |
// Set constraints | |
greenBox.anchor( | |
top: view.safeAreaLayoutGuide.topAnchor, | |
bottom: view.safeAreaLayoutGuide.bottomAnchor, | |
leading: view.leadingAnchor, | |
trailing: view.trailingAnchor, | |
padding: .init(top: 8, left: 8, bottom: 8, right: 8)) | |
redBox.anchor( | |
top: greenBox.topAnchor, | |
leading: greenBox.leadingAnchor, | |
trailing: greenBox.trailingAnchor, | |
padding: .init(top: 8, left: 8, bottom: 8, right: 8), | |
size: .init(width: 0, height: 100)) | |
self.view = view | |
} | |
} | |
// Present the view controller in the Live View window | |
PlaygroundPage.current.liveView = MyViewController() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment