Skip to content

Instantly share code, notes, and snippets.

@dermotos
Created February 1, 2018 05:29
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dermotos/51892d42fffc044e0d0dfa162980dac2 to your computer and use it in GitHub Desktop.
Save dermotos/51892d42fffc044e0d0dfa162980dac2 to your computer and use it in GitHub Desktop.
UIView extension to quickly add and fit or center a subview inside a superview, using constraints
class MyView : UIView {
func buildMyView() {
var indicatorView :UIActivityIndicatorView!
let indicatorSize : CGFloat = 120
loadingIndicator = UIView(frame: CGRect(origin: CGPoint.zero, size: CGSize(width: indicatorSize, height: indicatorSize)))
loadingIndicator.backgroundColor = UIColor.darkGray.withAlphaComponent(0.7)
loadingIndicator.layer.cornerRadius = 10.0
indicatorView = UIActivityIndicatorView(activityIndicatorStyle: .white)
indicatorView.hidesWhenStopped = false
let inset :CGFloat = 20.0
let insets = UIEdgeInsets(top: inset, left: inset, bottom: inset, right: inset)
loadingIndicator.fit(subView: indicatorView, .horizontallyAndVertically, with: insets)
view.center(subView: loadingIndicator, orientated: .horizontallyAndVertically, withOffsets: CGPoint(x: 0, y: -100))
}
}
//
// ViewExtensions.swift
//
// Created by Dermot O Sullivan on 31/1/17.
//
import Foundation
enum FitOrientation {
case horizontally
case vertically
case horizontallyAndVertically
}
extension UIView {
/** Center the provided view in current view by adding necessary constraints, and adding the view as a subview, if needed.
- parameter view: The subview to be added. Must have intrinsic size or height and width manually set.
*/
func center(subView view:UIView, orientated orientation:FitOrientation = .horizontallyAndVertically) {
center(subView: view, orientated: orientation, withOffsets: CGPoint.zero)
}
/** Center the provided view in current view by adding necessary constraints, and adding the view as a subview, if needed.
- parameter view: The subview to be added. Must have intrinsic size or height and width manually set.
- parameter offset: The offset from the center to use.
*/
func center(subView view:UIView, orientated orientation:FitOrientation = .horizontallyAndVertically, withOffsets offset:CGPoint) {
if view.superview != self {
self.addSubview(view)
}
view.translatesAutoresizingMaskIntoConstraints = false
if orientation == .horizontally || orientation == .horizontallyAndVertically {
let constraint = NSLayoutConstraint(item: view, attribute: .centerX, relatedBy: .equal, toItem: view.superview!, attribute: .centerX, multiplier: 1.0, constant: offset.x)
addConstraint(constraint)
}
if orientation == .vertically || orientation == .horizontallyAndVertically {
let constraint = NSLayoutConstraint(item: view, attribute: .centerY, relatedBy: .equal, toItem: view.superview!, attribute: .centerY, multiplier: 1.0, constant: offset.y)
addConstraint(constraint)
}
}
/** Fit the provided view in current view by adding necessary constraints, and adding the view as a subview, if needed */
func fit(subView view:UIView, _ orientation:FitOrientation = .horizontallyAndVertically, with padding:UIEdgeInsets = UIEdgeInsets.zero) {
if view.superview != self {
self.addSubview(view)
}
view.translatesAutoresizingMaskIntoConstraints = false
if orientation == .horizontally || orientation == .horizontallyAndVertically {
self.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-\(padding.left)-[subview]-\(padding.right)-|", options: .directionLeadingToTrailing, metrics: nil, views: ["subview": view]))
}
if orientation == .vertically || orientation == .horizontallyAndVertically {
self.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-\(padding.top)-[subview]-\(padding.bottom)-|", options: .directionLeadingToTrailing, metrics: nil, views: ["subview": view]))
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment