Skip to content

Commit

Permalink
Fix for fillSuperview from @danieladias
Browse files Browse the repository at this point in the history
  • Loading branch information
madhikarma committed Nov 17, 2014
1 parent 321396a commit e1fd0c8
Showing 1 changed file with 31 additions and 19 deletions.
50 changes: 31 additions & 19 deletions AutoLayoutHelper/UIView+LayoutHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,23 @@ import Foundation
import UIKit

/**
Extension of UIView for AutoLayout helper methods
Extension of UIView for AutoLayout helper methods
*/
extension UIView {



// Mark: - Fill

func fillSuperView(edges: UIEdgeInsets) -> [NSLayoutConstraint] {

var topConstraint: NSLayoutConstraint = self.addTopConstraint(toView: self.superview, relation: .Equal, constant: edges.top)
var leftConstraint: NSLayoutConstraint = self.addLeftConstraint(toView: self.superview, relation: .Equal, constant: edges.left)
var bottomConstraint: NSLayoutConstraint = self.addBottomConstraint(toView: self.superview, relation: .Equal, constant: edges.bottom)
var rightConstraint: NSLayoutConstraint = self.addRightConstraint(toView: self.superview, relation: .Equal, constant: edges.right)

return [topConstraint, leftConstraint, bottomConstraint, rightConstraint]
}


// MARK: - Left Constraints

Expand Down Expand Up @@ -66,20 +79,20 @@ extension UIView {

return self.addTopMarginConstraint(toView: view, attribute: .TopMargin, relation: relation, constant: constant)
}

func addTopMarginConstraint(toView view: UIView?, attribute: NSLayoutAttribute, relation: NSLayoutRelation, constant: CGFloat) -> NSLayoutConstraint {

let constraint: NSLayoutConstraint = self.createConstraint(attribute: .TopMargin, toView: view, attribute: attribute, relation: relation, constant: constant)
self.superview?.addConstraint(constraint)

return constraint
}


// MARK: - Bottom Constraints

func addBottomConstraint(toView view: UIView?, relation: NSLayoutRelation, constant: CGFloat) -> NSLayoutConstraint {

return self.addBottomConstraint(toView: view, attribute: .Bottom, relation: relation, constant: constant)
}

Expand All @@ -98,22 +111,22 @@ extension UIView {

return constraint
}

func addBottomMarginConstraint(toView view: UIView?, attribute: NSLayoutAttribute, relation: NSLayoutRelation, constant: CGFloat) -> NSLayoutConstraint {

return self.addBottomConstraint(toView: view, attribute: .BottomMargin, relation: relation, constant: constant)
}


// MARK: - Center X Constraint

func addCenterXConstraint(toView view: UIView?) -> NSLayoutConstraint {

func addCenterXConstraint(toView view: UIView?) -> NSLayoutConstraint {

return self.addCenterXConstraint(toView: view, relation: .Equal, constant: 0)
}

func addCenterXConstraint(toView view: UIView?, constant: CGFloat) -> NSLayoutConstraint {

return self.addCenterXConstraint(toView: view, relation: .Equal, constant: constant)
}

Expand All @@ -125,7 +138,7 @@ extension UIView {
return constraint
}


// MARK: - Center Y Constraint

func addCenterYConstraint(toView view: UIView?) -> NSLayoutConstraint {
Expand All @@ -145,7 +158,7 @@ extension UIView {

return constraint
}


// MARK: - Width Constraints

Expand All @@ -156,12 +169,12 @@ extension UIView {

return constraint
}

func addWidthConstraint(relation relation1: NSLayoutRelation, constant: CGFloat) -> NSLayoutConstraint {

return self.addWidthConstraint(toView: nil, relation: relation1, constant: constant)
}


// MARK: - Height Constraints

Expand All @@ -171,19 +184,19 @@ extension UIView {
self.superview?.addConstraint(constraint)

return constraint

}

func addHeightConstraint(relation relation1: NSLayoutRelation, constant: CGFloat) -> NSLayoutConstraint {

return self.addHeightConstraint(toView: nil, relation: relation1, constant: constant)
}


// MARK: - Private

private func createConstraint(attribute attr1: NSLayoutAttribute, toView: UIView?, attribute attr2: NSLayoutAttribute, relation: NSLayoutRelation, constant: CGFloat) -> NSLayoutConstraint {

let constraint = NSLayoutConstraint(
item: self,
attribute: attr1,
Expand All @@ -196,4 +209,3 @@ extension UIView {
return constraint
}
}

0 comments on commit e1fd0c8

Please sign in to comment.