Skip to content

Instantly share code, notes, and snippets.

@seanhenry
Last active November 27, 2017 02:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seanhenry/ecc62180924217749a84f948ae8bd8c8 to your computer and use it in GitHub Desktop.
Save seanhenry/ecc62180924217749a84f948ae8bd8c8 to your computer and use it in GitHub Desktop.
SO: Out of bounds UIView constraints programmatically
//: A UIKit based Playground for presenting user interface
import UIKit
import PlaygroundSupport
let vc = UIViewController()
let view = vc.view!
PlaygroundPage.current.liveView = vc
view.layoutIfNeeded()
func setupViewConstraints(item:UIView, leadingTo:NSLayoutXAxisAnchor, leadingCon:CGFloat,
trailingTo:NSLayoutXAxisAnchor, trailingCon:CGFloat, topTo:NSLayoutYAxisAnchor,
topCon:CGFloat, bottomTo:NSLayoutYAxisAnchor, bottomCon:CGFloat) {
item.translatesAutoresizingMaskIntoConstraints = false
let leading = item.leadingAnchor.constraint(equalTo: leadingTo, constant: leadingCon)
leading.priority = UILayoutPriority(999)
leading.isActive = true
trailingTo.constraint(equalTo: item.trailingAnchor, constant: trailingCon).isActive = true
item.topAnchor.constraint(equalTo: topTo, constant:topCon).isActive = true
bottomTo.constraint(equalTo: item.bottomAnchor, constant:bottomCon).isActive = true
}
let width = (view.frame.width)*0.8
let red = UIView()
red.backgroundColor = .red
view.addSubview(red)
setupViewConstraints(item: red, leadingTo: view.leadingAnchor, leadingCon: 0, trailingTo: view.trailingAnchor, trailingCon: (view.frame.width)*0.2, topTo: view.topAnchor, topCon: 0, bottomTo: view.bottomAnchor, bottomCon: (view.frame.width)*0.8)
let blue = UIView()
blue.backgroundColor = .blue
view.addSubview(blue)
setupViewConstraints(item: blue, leadingTo: view.leadingAnchor, leadingCon: 0, trailingTo: view.trailingAnchor, trailingCon: (view.frame.width)*0.2, topTo: red.bottomAnchor, topCon: 0, bottomTo: view.bottomAnchor, bottomCon: 0)
let yellow = UIView()
yellow.backgroundColor = .yellow
yellow.alpha = 0.8
view.addSubview(yellow)
setupViewConstraints(item: yellow, leadingTo: view.leadingAnchor, leadingCon: 0, trailingTo: view.trailingAnchor, trailingCon: 0, topTo: view.topAnchor, topCon: 0, bottomTo: view.bottomAnchor, bottomCon: 0)
var leadingConstraint = view.constraints.first { c in
return c.firstAnchor == yellow.leadingAnchor
&& c.secondAnchor == view.leadingAnchor
}!
var trailingConstraint = view.constraints.first { c in
return c.secondAnchor == yellow.trailingAnchor
&& c.firstAnchor == view.trailingAnchor
}!
var hamburgerMenuIsVisible = false
func hamburgerBtnPressed() {
let menuWidth = (view.frame.width)*0.8
hamburgerMenuIsVisible = !hamburgerMenuIsVisible
leadingConstraint.constant = hamburgerMenuIsVisible ? menuWidth : 0
trailingConstraint.constant = hamburgerMenuIsVisible ? -menuWidth : 0
UIView.animate(withDuration: 0.2, delay:0.0, options: .curveEaseIn, animations: {
view.layoutIfNeeded()
}) { (animationComplete) in
print("Animation is complete!")
}
}
Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { (_) in
hamburgerBtnPressed()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment