Skip to content

Instantly share code, notes, and snippets.

@yoxisem544
Created July 1, 2022 10:06
Show Gist options
  • Save yoxisem544/f6f88d3cfc23d99c8c9471c9093a3681 to your computer and use it in GitHub Desktop.
Save yoxisem544/f6f88d3cfc23d99c8c9471c9093a3681 to your computer and use it in GitHub Desktop.
import UIKit
extension UIView {
static func vstack(
_ views: UIView...,
spacing: CGFloat = 0,
alignment: UIStackView.Alignment = .fill,
distribution: UIStackView.Distribution = .fill
) -> UIStackView {
let stackView = UIStackView(arrangedSubviews: views)
stackView.axis = .vertical
stackView.spacing = spacing
stackView.alignment = alignment
stackView.distribution = distribution
return stackView
}
static func vstack(
_ views: [UIView],
spacing: CGFloat = 0,
alignment: UIStackView.Alignment = .fill,
distribution: UIStackView.Distribution = .fill
) -> UIStackView {
let stackView = UIStackView(arrangedSubviews: views)
stackView.axis = .vertical
stackView.spacing = spacing
stackView.alignment = alignment
stackView.distribution = distribution
return stackView
}
static func hstack(
_ views: UIView...,
spacing: CGFloat = 0,
alignment: UIStackView.Alignment = .fill,
distribution: UIStackView.Distribution = .fill
) -> UIStackView {
let stackView = UIStackView(arrangedSubviews: views)
stackView.axis = .horizontal
stackView.spacing = spacing
stackView.alignment = alignment
stackView.distribution = distribution
return stackView
}
static func hstack(
_ views: [UIView],
spacing: CGFloat = 0,
alignment: UIStackView.Alignment = .fill,
distribution: UIStackView.Distribution = .fill
) -> UIStackView {
let stackView = UIStackView(arrangedSubviews: views)
stackView.axis = .horizontal
stackView.spacing = spacing
stackView.alignment = alignment
stackView.distribution = distribution
return stackView
}
}
extension UIStackView {
@discardableResult
func setAlignment(_ alignment: UIStackView.Alignment) -> UIStackView {
self.alignment = alignment
return self
}
@discardableResult
func setSpacing(_ spacing: CGFloat) -> UIStackView {
self.spacing = spacing
return self
}
@discardableResult
func setDistribution(_ distribution: UIStackView.Distribution) -> UIStackView {
self.distribution = distribution
return self
}
}
extension UIView {
fileprivate func _stack(
_ axis: NSLayoutConstraint.Axis,
views: [UIView],
spacing: CGFloat = 0,
alignment: UIStackView.Alignment = .fill,
distribution: UIStackView.Distribution = .fill
) -> UIStackView {
let stackView = UIStackView(arrangedSubviews: views)
stackView.axis = axis
stackView.spacing = spacing
stackView.alignment = alignment
stackView.distribution = distribution
addSubview(stackView)
stackView.fillSuperview()
return stackView
}
@discardableResult
open func stack(
_ views: UIView...,
spacing: CGFloat = 0,
alignment: UIStackView.Alignment = .fill,
distribution: UIStackView.Distribution = .fill
) -> UIStackView {
_stack(
.vertical,
views: views,
spacing: spacing,
alignment: alignment,
distribution: distribution
)
}
@discardableResult
open func hstack(
_ views: UIView...,
spacing: CGFloat = 0,
alignment: UIStackView.Alignment = .fill,
distribution: UIStackView.Distribution = .fill
) -> UIStackView {
_stack(
.horizontal,
views: views,
spacing: spacing,
alignment: alignment,
distribution: distribution
)
}
@discardableResult
open func withHeight(_ value: CGFloat) -> Self {
translatesAutoresizingMaskIntoConstraints = false
heightAnchor.constraint(equalToConstant: value).isActive = true
return self
}
@discardableResult
open func withWidth(_ value: CGFloat) -> Self {
translatesAutoresizingMaskIntoConstraints = false
widthAnchor.constraint(equalToConstant: value).isActive = true
return self
}
@discardableResult
open func withSize(_ size: CGSize) -> Self {
withWidth(size.width).withHeight(size.height)
}
@discardableResult
open func withBorder(width: CGFloat, color: UIColor) -> Self {
layer.borderWidth = width
layer.borderColor = color.cgColor
return self
}
}
extension CGSize {
public static func equalEdge(_ edge: CGFloat) -> CGSize {
CGSize(width: edge, height: edge)
}
}
extension UIEdgeInsets {
public static func allSides(_ side: CGFloat) -> UIEdgeInsets {
.init(top: side, left: side, bottom: side, right: side)
}
}
extension UIImageView {
public convenience init(image: UIImage?, contentMode: UIView.ContentMode = .scaleAspectFill) {
self.init(image: image)
self.contentMode = contentMode
self.clipsToBounds = true
}
}
extension UIStackView {
@discardableResult
open func withMargins(_ margins: UIEdgeInsets) -> UIStackView {
self.layoutMargins = margins
isLayoutMarginsRelativeArrangement = true
return self
}
@discardableResult
open func padLeft(_ left: CGFloat) -> UIStackView {
isLayoutMarginsRelativeArrangement = true
layoutMargins.left = left
return self
}
@discardableResult
open func padRight(_ right: CGFloat) -> UIStackView {
isLayoutMarginsRelativeArrangement = true
layoutMargins.right = right
return self
}
@discardableResult
open func padTop(_ top: CGFloat) -> UIStackView {
isLayoutMarginsRelativeArrangement = true
layoutMargins.top = top
return self
}
@discardableResult
open func padBottom(_ bottom: CGFloat) -> UIStackView {
isLayoutMarginsRelativeArrangement = true
layoutMargins.bottom = bottom
return self
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment