Last active
June 19, 2020 16:51
-
-
Save rsaenzi/c2d70658e9d502add75c2382d2ded6a6 to your computer and use it in GitHub Desktop.
To create a copy of a constraint, with a different multiplier
This file contains 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
// | |
// NSLayoutConstraint+Multiplier.swift | |
// | |
// Created by Rigoberto Sáenz Imbacuán (https://www.linkedin.com/in/rsaenzi/) | |
// Copyright © 2017. All rights reserved. | |
// | |
extension NSLayoutConstraint { | |
@discardableResult | |
static func change(_ constraint: NSLayoutConstraint, newMultiplier: CGFloat) -> NSLayoutConstraint { | |
// Creates a copy of the constraint | |
let newConstraint = NSLayoutConstraint( | |
item: constraint.firstItem, | |
attribute: constraint.firstAttribute, | |
relatedBy: constraint.relation, | |
toItem: constraint.secondItem, | |
attribute: constraint.secondAttribute, | |
multiplier: newMultiplier, | |
constant: constraint.constant | |
) | |
newConstraint.priority = constraint.priority | |
// Then switch them | |
NSLayoutConstraint.deactivate([constraint]) | |
NSLayoutConstraint.activate([newConstraint]) | |
return newConstraint | |
} | |
static func trailing(_ view: UIView, to item: UIView) -> NSLayoutConstraint { | |
let trailing = NSLayoutConstraint ( | |
item: view, | |
attribute: .trailing, | |
relatedBy: .equal, | |
toItem: item, | |
attribute: .trailing, | |
multiplier: 1, | |
constant: 0 | |
) | |
return trailing | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment