Skip to content

Instantly share code, notes, and snippets.

@rafattouqir
Created January 23, 2020 05:04
Show Gist options
  • Save rafattouqir/735b8a3a851bf9928e95660ecde408f2 to your computer and use it in GitHub Desktop.
Save rafattouqir/735b8a3a851bf9928e95660ecde408f2 to your computer and use it in GitHub Desktop.
import UIKit
// MARK: - SafeArea Extensions
extension UIView {
var safeTopAnchor: NSLayoutYAxisAnchor {
if #available(iOS 11.0, *) {
return self.safeAreaLayoutGuide.topAnchor
}
return self.topAnchor
}
var safeLeadingAnchor: NSLayoutXAxisAnchor {
if #available(iOS 11.0, *) {
return self.safeAreaLayoutGuide.leadingAnchor
}
return self.leadingAnchor
}
var safeTrailingAnchor: NSLayoutXAxisAnchor {
if #available(iOS 11.0, *) {
return self.safeAreaLayoutGuide.trailingAnchor
}
return self.trailingAnchor
}
var safeBottomAnchor: NSLayoutYAxisAnchor {
if #available(iOS 11.0, *) {
return self.safeAreaLayoutGuide.bottomAnchor
}
return self.bottomAnchor
}
func safeBoundInside(superView: UIView, inset: UIEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)) {
self.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
self.safeLeadingAnchor.constraint(equalTo: superView.safeLeadingAnchor, constant: inset.left),
superView.safeTrailingAnchor.constraint(equalTo: self.safeTrailingAnchor, constant: inset.right),
self.safeTopAnchor.constraint(equalTo: superView.safeTopAnchor, constant: inset.top),
superView.safeBottomAnchor.constraint(equalTo: self.safeBottomAnchor, constant: inset.bottom)
])
}
}
// MARK: - VFL Extensions
extension UIView {
func addConstraintsWithFormat(
format: String, views: UIView...,
options: NSLayoutConstraint.FormatOptions = NSLayoutConstraint.FormatOptions()) {
var viewsDictionary = [String: UIView]()
for (index, view) in views.enumerated() {
let key = "v\(index)"
viewsDictionary[key] = view
view.translatesAutoresizingMaskIntoConstraints = false
}
addConstraints(NSLayoutConstraint.constraints(withVisualFormat: format, options: options, metrics: nil, views: viewsDictionary))
}
func boundInside(superView: UIView,
inset: UIEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0),
unpin: UIRectEdge = []) {
self.translatesAutoresizingMaskIntoConstraints = false
let leftUnpin = (unpin == .left) ? ">=" : ""
let rightUnpin = (unpin == .right) ? ">=" : ""
let topUnpin = (unpin == .top) ? ">=" : ""
let bottomUnpin = (unpin == .bottom) ? ">=" : ""
superView.addConstraints(
NSLayoutConstraint.constraints(
withVisualFormat: "H:|-(\(leftUnpin)\(inset.left))-[subview]-(\(rightUnpin)\(inset.right))-|",
options: NSLayoutConstraint.FormatOptions.directionLeadingToTrailing,
metrics: nil, views: ["subview": self])
)
superView.addConstraints(
NSLayoutConstraint.constraints(withVisualFormat: "V:|-(\(topUnpin)\(inset.top))-[subview]-(\(bottomUnpin)\(inset.bottom))-|",
options: NSLayoutConstraint.FormatOptions.directionLeadingToTrailing,
metrics: nil, views: ["subview": self])
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment