Last active
April 18, 2024 16:03
-
-
Save Nadohs/ba7f427205d930c5e58e500b753a684c to your computer and use it in GitHub Desktop.
constaints_helper.swift
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 Foundation | |
import UIKit | |
extension UIButton { | |
func setImage(named: String) { | |
if let image = UIImage(named: named) { | |
self.setImage(image, for: .normal) | |
} | |
} | |
} | |
extension UIImageView { | |
func setImage(named: String) { | |
if let image = UIImage(named: named) { | |
self.image = image | |
} | |
} | |
} | |
struct Insets { | |
let left: CGFloat | |
let top: CGFloat | |
let right: CGFloat | |
let bottom: CGFloat | |
static let zero = Insets(left: 0, top: 0, right: 0, bottom: 0) | |
} | |
extension UIView { | |
func bindFrameToSuperviewBounds(insets: Insets? = nil) { | |
guard let superview = self.superview else { | |
debug("Error! `superview` was nil – call `addSubview(view: UIView)` before calling `bindFrameToSuperviewBounds()` to fix this.") | |
return | |
} | |
self.translatesAutoresizingMaskIntoConstraints = false | |
self.topAnchor.constraint(equalTo: superview.topAnchor, constant: insets?.top ?? 0).isActive = true | |
self.bottomAnchor.constraint(equalTo: superview.bottomAnchor, constant: insets?.bottom ?? 0).isActive = true | |
self.leadingAnchor.constraint(equalTo: superview.leadingAnchor, constant: insets?.left ?? 0).isActive = true | |
self.trailingAnchor.constraint(equalTo: superview.trailingAnchor, constant: insets?.right ?? 0).isActive = true | |
} | |
func bindFrameToSuperviewHorizontal(insets: Insets? = nil) { | |
guard let superview = self.superview else { | |
debug("Error! `superview` was nil – call `addSubview(view: UIView)` before calling `bindFrameToSuperviewBounds()` to fix this.") | |
return | |
} | |
self.translatesAutoresizingMaskIntoConstraints = false | |
self.leadingAnchor.constraint(equalTo: superview.leadingAnchor, constant: insets?.left ?? 0).isActive = true | |
self.trailingAnchor.constraint(equalTo: superview.trailingAnchor, constant: insets?.right ?? 0).isActive = true | |
} | |
func bindFrameWidthOfHorizontal(targetView: UIView, insets: Insets? = nil) { | |
self.translatesAutoresizingMaskIntoConstraints = false | |
self.leadingAnchor.constraint(equalTo: targetView.leadingAnchor, constant: insets?.left ?? 0).isActive = true | |
self.trailingAnchor.constraint(equalTo: targetView.trailingAnchor, constant: insets?.right ?? 0).isActive = true | |
} | |
func bindFrameToSuperviewVertical(insets: Insets? = nil) { | |
guard let superview = self.superview else { | |
debug("Error! `superview` was nil – call `addSubview(view: UIView)` before calling `bindFrameToSuperviewBounds()` to fix this.") | |
return | |
} | |
self.translatesAutoresizingMaskIntoConstraints = false | |
self.topAnchor.constraint(equalTo: superview.topAnchor, constant: insets?.top ?? 0).isActive = true | |
self.bottomAnchor.constraint(equalTo: superview.bottomAnchor, constant: insets?.bottom ?? 0).isActive = true | |
} | |
func fixedWidth(_ width: CGFloat) -> NSLayoutConstraint { | |
return NSLayoutConstraint(item: self, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .width, multiplier: 1, constant: width) | |
} | |
func minWidth(_ width: CGFloat) -> NSLayoutConstraint { | |
return NSLayoutConstraint(item: self, attribute: .width, relatedBy: .greaterThanOrEqual, toItem: nil, attribute: .width, multiplier: 1, constant: width) | |
} | |
func fixedHeight(_ height: CGFloat) -> NSLayoutConstraint { | |
return NSLayoutConstraint(item: self, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .width, multiplier: 1, constant: height) | |
} | |
func snapOnRight(to: UIView, constant: CGFloat=0) -> NSLayoutConstraint { | |
return NSLayoutConstraint(item: to, | |
attribute: NSLayoutConstraint.Attribute.leading, | |
relatedBy: .equal, | |
toItem: self, | |
attribute: .trailing, | |
multiplier: 1, | |
constant: constant) | |
} | |
func snapOnTop(to: UIView, constant: CGFloat=0) -> NSLayoutConstraint { | |
return NSLayoutConstraint(item: to, | |
attribute: NSLayoutConstraint.Attribute.top, | |
relatedBy: .equal, | |
toItem: self, | |
attribute: .bottom, | |
multiplier: 1, | |
constant: constant) | |
} | |
func snapLeft(to: UIView, constant: CGFloat=0) -> NSLayoutConstraint { | |
return NSLayoutConstraint(item: self, | |
attribute: NSLayoutConstraint.Attribute.leading, | |
relatedBy: .equal, | |
toItem: to, | |
attribute: .leading, | |
multiplier: 1, | |
constant: constant) | |
} | |
func snapRight(to: UIView, constant: CGFloat=0) -> NSLayoutConstraint { | |
return NSLayoutConstraint(item: self, | |
attribute: NSLayoutConstraint.Attribute.trailing, | |
relatedBy: .equal, | |
toItem: to, | |
attribute: .trailing, | |
multiplier: 1, | |
constant: constant) | |
} | |
func snapBottom(to: UIView, constant: CGFloat=0) -> NSLayoutConstraint { | |
return NSLayoutConstraint(item: self, | |
attribute: NSLayoutConstraint.Attribute.bottom, | |
relatedBy: .equal, | |
toItem: to, | |
attribute: .bottom, | |
multiplier: 1, | |
constant: constant) | |
} | |
func snapTop(to: UIView, constant: CGFloat=0) -> NSLayoutConstraint { | |
return NSLayoutConstraint(item: self, | |
attribute: NSLayoutConstraint.Attribute.top, | |
relatedBy: .equal, | |
toItem: to, | |
attribute: .top, | |
multiplier: 1, | |
constant: constant) | |
} | |
func centerYinside(to: UIView, constant: CGFloat=0) -> NSLayoutConstraint { | |
return NSLayoutConstraint(item: self, | |
attribute: NSLayoutConstraint.Attribute.centerY, | |
relatedBy: .equal, | |
toItem: to, | |
attribute: NSLayoutConstraint.Attribute.centerY, | |
multiplier: 1, | |
constant: constant) | |
} | |
func centerXinside(to: UIView, constant: CGFloat=0) -> NSLayoutConstraint { | |
return NSLayoutConstraint(item: self, | |
attribute: NSLayoutConstraint.Attribute.centerX, | |
relatedBy: .equal, | |
toItem: to, | |
attribute: NSLayoutConstraint.Attribute.centerX, | |
multiplier: 1, | |
constant: constant) | |
} | |
func constraint(to: UIView, attribute:NSLayoutConstraint.Attribute, constant: CGFloat=0, multiplier: CGFloat = 1) -> NSLayoutConstraint { | |
return NSLayoutConstraint(item: self, | |
attribute: attribute, | |
relatedBy: .equal, | |
toItem: to, | |
attribute: attribute, | |
multiplier: multiplier, | |
constant: constant) | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment