-
Notifications
You must be signed in to change notification settings - Fork 0
UIKit extensions
Amr Aboelela edited this page Oct 1, 2021
·
4 revisions
##UIView
// Apply constraints to current view relative to another view, by default it is superview.
public func applyConstraints(
relativeToView view: UIView? = nil,
leading: CGFloat? = nil,
trailing: CGFloat? = nil,
top: CGFloat? = nil,
bottom: CGFloat? = nil,
centerX: CGFloat? = nil,
centerY: CGFloat? = nil,
width: CGFloat? = nil,
height: CGFloat? = nil
) {
self.translatesAutoresizingMaskIntoConstraints = false
var parentView = view
if parentView == nil {
parentView = self.superview
}
if let view = parentView {
if let leading = leading {
leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: leading).isActive = true
}
if let trailing = trailing {
trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: trailing).isActive = true
}
if let top = top {
topAnchor.constraint(equalTo: view.topAnchor, constant: top).isActive = true
}
if let bottom = bottom {
bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: bottom).isActive = true
}
if let centerX = centerX {
centerXAnchor.constraint(equalTo: view.centerXAnchor, constant: centerX).isActive = true
}
if let centerY = centerY {
centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: centerY).isActive = true
}
}
if let width = width {
widthAnchor.constraint(equalToConstant: width).isActive = true
}
if let height = height {
heightAnchor.constraint(equalToConstant: height).isActive = true
}
}
// Apply constraints to current view relative to another anchor.
public func applyConstraints(
leadingAnchor: NSLayoutAnchor<NSLayoutXAxisAnchor>? = nil,
leadingValue: CGFloat? = nil,
trailingAnchor: NSLayoutAnchor<NSLayoutXAxisAnchor>? = nil,
trailingValue: CGFloat? = nil,
topAnchor: NSLayoutAnchor<NSLayoutYAxisAnchor>? = nil,
topValue: CGFloat? = nil,
bottomAnchor: NSLayoutAnchor<NSLayoutYAxisAnchor>? = nil,
bottomValue: CGFloat? = nil,
centerXAnchor: NSLayoutAnchor<NSLayoutXAxisAnchor>? = nil,
centerXValue: CGFloat? = nil,
centerYAnchor: NSLayoutAnchor<NSLayoutYAxisAnchor>? = nil,
centerYValue: CGFloat? = nil,
widthAnchor: NSLayoutAnchor<NSLayoutDimension>? = nil,
widthValue: CGFloat? = nil,
heightAnchor: NSLayoutAnchor<NSLayoutDimension>? = nil,
heightValue: CGFloat? = nil
) {
self.translatesAutoresizingMaskIntoConstraints = false
if let topAnchor = topAnchor {
self.topAnchor.constraint(equalTo: topAnchor, constant: topValue ?? 0).isActive = true
}
if let bottomAnchor = bottomAnchor {
self.bottomAnchor.constraint(equalTo: bottomAnchor, constant: bottomValue ?? 0).isActive = true
}
if let leadingAnchor = leadingAnchor {
self.leadingAnchor.constraint(equalTo: leadingAnchor, constant: leadingValue ?? 0).isActive = true
}
if let trailingAnchor = trailingAnchor {
self.trailingAnchor.constraint(equalTo: trailingAnchor, constant: trailingValue ?? 0).isActive = true
}
if let widthAnchor = widthAnchor {
self.widthAnchor.constraint(equalTo: widthAnchor, constant: widthValue ?? 0).isActive = true
}
if let heightAnchor = heightAnchor {
self.heightAnchor.constraint(equalTo: heightAnchor, constant: heightValue ?? 0).isActive = true
}
if let centerXAnchor = centerXAnchor {
self.centerXAnchor.constraint(equalTo: centerXAnchor, constant: centerXValue ?? 0).isActive = true
}
if let centerYAnchor = centerYAnchor {
self.centerYAnchor.constraint(equalTo: centerYAnchor, constant: centerYValue ?? 0).isActive = true
}
}