Last active
April 16, 2016 09:29
-
-
Save khanhldt/40bc27ee157f1be1e3c4557a6a6f1799 to your computer and use it in GitHub Desktop.
Use XibLayoutConstraintHelper to change the constraint of a view from to the Top Guide instead of Top View, especially useful for xib file development.
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
// | |
// Created by Khanh Le Do on 16/4/16. | |
// Copyright © 2016 kloc. All rights reserved. | |
// | |
import Foundation | |
import UIKit | |
public class LayoutConstraintBuilder { | |
private var mOrigin: NSLayoutConstraint? | |
private var mFirstItem: AnyObject? | |
private var mSecondItem: AnyObject? | |
private var mPriority: UILayoutPriority? | |
private var mRelation: NSLayoutRelation? | |
private var mMultiplier: CGFloat? | |
private var mConstant: CGFloat? | |
private var mFirstAttribute: NSLayoutAttribute? | |
private var mSecondAttribute: NSLayoutAttribute? | |
public init() { | |
// Nothing to init | |
} | |
public init(origin: NSLayoutConstraint) { | |
self.mOrigin = origin | |
} | |
public func setFirstItem(firstItem: AnyObject?) -> LayoutConstraintBuilder { | |
self.mFirstItem = firstItem | |
return self | |
} | |
public func setSecondItem(secondItem: AnyObject?) -> LayoutConstraintBuilder { | |
self.mSecondItem = secondItem | |
return self | |
} | |
public func setPriority(priority: UILayoutPriority?) -> LayoutConstraintBuilder { | |
self.mPriority = priority | |
return self | |
} | |
public func setRelation(relation: NSLayoutRelation?) -> LayoutConstraintBuilder { | |
self.mRelation = relation | |
return self | |
} | |
public func setMultiplier(multiplier: CGFloat?) -> LayoutConstraintBuilder { | |
self.mMultiplier = multiplier | |
return self | |
} | |
public func setConstant(constant: CGFloat?) -> LayoutConstraintBuilder { | |
self.mConstant = constant | |
return self | |
} | |
public func setFirstAttribute(firstAttribute: NSLayoutAttribute?) -> LayoutConstraintBuilder { | |
self.mFirstAttribute = firstAttribute | |
return self | |
} | |
public func setSecondAttribute(secondAttribute: NSLayoutAttribute?) -> LayoutConstraintBuilder { | |
self.mSecondAttribute = secondAttribute | |
return self | |
} | |
public func build() -> NSLayoutConstraint { | |
let priority = (self.mPriority != nil ? self.mPriority : self.mOrigin?.priority) | |
let relation = (self.mRelation != nil ? self.mRelation : self.mOrigin?.relation) | |
let multiplier = (self.mMultiplier != nil ? self.mMultiplier : self.mOrigin?.multiplier) | |
let constant = (self.mConstant != nil ? self.mConstant : self.mOrigin?.constant) | |
let firstAttribute = (self.mFirstAttribute != nil ? self.mFirstAttribute : self.mOrigin?.firstAttribute) | |
let secondAttribute = (self.mSecondAttribute != nil ? self.mSecondAttribute : self.mOrigin?.secondAttribute) | |
let firstItem = (self.mFirstItem != nil ? self.mFirstItem : self.mOrigin?.firstItem) | |
let secondItem = (self.mSecondItem != nil ? self.mSecondItem : self.mOrigin?.secondItem) | |
let ret = NSLayoutConstraint(item: firstItem!, attribute: firstAttribute!, relatedBy: relation!, toItem: secondItem, attribute: secondAttribute!, multiplier: multiplier!, constant: constant!) | |
ret.priority = priority! | |
return ret | |
} | |
} |
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
// | |
// Created by Khanh Le Do on 16/4/16. | |
// Copyright © 2016 kloc. All rights reserved. | |
// | |
import Foundation | |
import UIKit | |
/// If using xib files, it would be not possible to set up the constraints for the view to be located under the Top Guide or above the Bottom Guide. Here we shall provide utility helper to patch the constraints of the view from against Edge to Guide. | |
public final class XibLayoutConstraintHelper { | |
// MARK: - Public Methods | |
public static func patchTopEdgeToTopGuide(viewController: UIViewController) { | |
let rootView = viewController.view | |
for constraint in rootView.constraints { | |
var constraintBuilder: LayoutConstraintBuilder? | |
// Find the Top Edge constraint to the root view & replace it with the Top Guide | |
if (constraint.firstItem as? UIView == rootView && constraint.firstAttribute == .Top) { | |
constraintBuilder = LayoutConstraintBuilder(origin: constraint) | |
.setFirstItem(viewController.topLayoutGuide) | |
.setFirstAttribute(NSLayoutAttribute.Bottom) | |
} else if (constraint.secondItem as? UIView == rootView && constraint.secondAttribute == .Top) { | |
constraintBuilder = LayoutConstraintBuilder(origin: constraint) | |
.setSecondItem(viewController.topLayoutGuide) | |
.setSecondAttribute(NSLayoutAttribute.Bottom) | |
} | |
if constraintBuilder != nil { | |
// Found it, now we perform the replacement | |
rootView.removeConstraint(constraint) | |
rootView.addConstraint(constraintBuilder!.build()) | |
} | |
} | |
} | |
// MARK: - Private | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment