Created
October 26, 2015 05:13
-
-
Save glennstephens/1937d94cd8b35305f874 to your computer and use it in GitHub Desktop.
An alternative to working with constraints in code using a nice fluent style interface. A work in progress, but hopefully helps
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
// // Just an example of making a fluent style interface. It's a work in progress but I'd rather not use the NSLayoutContraint code | |
// // As it seems like a poorly designed API. I've wrapped it with some fluent style access for common issues | |
// | |
// var mainButton = new UIButton (); | |
// mainButton.SetTitle ("Click me dude", UIControlState.Normal); | |
// mainButton.BackgroundColor = UIColor.Blue; | |
// Add (mainButton); | |
// | |
// var secondButton = new UIButton (); | |
// secondButton.SetTitle ("The other one", UIControlState.Normal); | |
// secondButton.BackgroundColor = UIColor.Red; | |
// Add (secondButton); | |
// | |
// mainButton | |
// .ConstrainCenterXToParent (-100) | |
// .ConstrainCenterYToParent (0) | |
// .ConstrainWidth (150); | |
// | |
// secondButton | |
// .ConstrainHeight (80) | |
// .ConstrainLeftToView (mainButton, 20) | |
// .ConstrainRightToParent (-10) | |
// .ConstrainTopToView (mainButton, 10); | |
public static class ConstraintsHelper | |
{ | |
public static void EnsureCorrectMode(UIView view) | |
{ | |
if (view.TranslatesAutoresizingMaskIntoConstraints) | |
view.TranslatesAutoresizingMaskIntoConstraints = false; | |
} | |
static void AddConstraint(UIView view1, NSLayoutAttribute attribute1, NSLayoutRelation relation, | |
UIView view2, NSLayoutAttribute attribute2, nfloat multiplier, nfloat constant) | |
{ | |
if (view1 != null) | |
EnsureCorrectMode (view1); | |
if (view2 != null) | |
EnsureCorrectMode (view2); | |
var constraint = NSLayoutConstraint.Create (view1, attribute1, relation, | |
view2, attribute2, multiplier, constant); | |
view1.Superview.AddConstraint (constraint); | |
} | |
public static UIView ConstrainLeftToParent(this UIView view, nfloat gap) | |
{ | |
AddConstraint (view, | |
NSLayoutAttribute.Left, NSLayoutRelation.Equal, | |
view.Superview, NSLayoutAttribute.Left, 1, gap); | |
return view; | |
} | |
public static UIView ConstrainRightToParent(this UIView view, nfloat gap) | |
{ | |
AddConstraint (view, | |
NSLayoutAttribute.Right, NSLayoutRelation.Equal, | |
view.Superview, NSLayoutAttribute.Right, 1, gap); | |
return view; | |
} | |
public static UIView ConstrainTopToParent(this UIView view, nfloat gap) | |
{ | |
AddConstraint (view, | |
NSLayoutAttribute.Top, NSLayoutRelation.Equal, | |
view.Superview, NSLayoutAttribute.Top, 1, gap); | |
return view; | |
} | |
public static UIView ConstrainBottomToParent(this UIView view, nfloat gap) | |
{ | |
AddConstraint (view, | |
NSLayoutAttribute.Bottom, NSLayoutRelation.Equal, | |
view.Superview, NSLayoutAttribute.Bottom, 1, gap); | |
return view; | |
} | |
public static UIView ConstrainLeftToView(this UIView view, UIView otherView, nfloat gap) | |
{ | |
AddConstraint (view, | |
NSLayoutAttribute.Left, NSLayoutRelation.Equal, | |
otherView, NSLayoutAttribute.Right, 1, gap); | |
return view; | |
} | |
public static UIView ConstrainRightToView(this UIView view, UIView otherView, nfloat gap) | |
{ | |
AddConstraint (view, | |
NSLayoutAttribute.Right, NSLayoutRelation.Equal, | |
otherView, NSLayoutAttribute.Left, 1, gap); | |
return view; | |
} | |
public static UIView ConstrainTopToView(this UIView view, UIView otherView, nfloat gap) | |
{ | |
AddConstraint (view, | |
NSLayoutAttribute.Top, NSLayoutRelation.Equal, | |
otherView, NSLayoutAttribute.Bottom, 1, gap); | |
return view; | |
} | |
public static UIView ConstrainBottomToView(this UIView view, UIView otherView, nfloat gap) | |
{ | |
AddConstraint (view, | |
NSLayoutAttribute.Bottom, NSLayoutRelation.Equal, | |
otherView, NSLayoutAttribute.Top, 1, gap); | |
return view; | |
} | |
public static UIView ConstrainHeight(this UIView view, nfloat height) | |
{ | |
AddConstraint (view, | |
NSLayoutAttribute.Height, NSLayoutRelation.Equal, | |
view, NSLayoutAttribute.Height, 1f, height); | |
return view; | |
} | |
public static UIView ConstrainWidth(this UIView view, nfloat width) | |
{ | |
AddConstraint (view, | |
NSLayoutAttribute.Width, NSLayoutRelation.Equal, | |
view, NSLayoutAttribute.Width, 1f, width); | |
return view; | |
} | |
public static UIView KeepWidth(this UIView view) | |
{ | |
AddConstraint (view, | |
NSLayoutAttribute.Width, NSLayoutRelation.Equal, | |
view, NSLayoutAttribute.Width, 1f, view.Frame.Width); | |
return view; | |
} | |
public static UIView KeepHeight(this UIView view) | |
{ | |
AddConstraint (view, | |
NSLayoutAttribute.Height, NSLayoutRelation.Equal, | |
view, NSLayoutAttribute.Height, 1f, view.Frame.Height); | |
return view; | |
} | |
public static UIView KeepWidthHeight(this UIView view) | |
{ | |
AddConstraint (view, | |
NSLayoutAttribute.Width, NSLayoutRelation.Equal, | |
view, NSLayoutAttribute.Width, 1f, view.Frame.Width); | |
AddConstraint (view, | |
NSLayoutAttribute.Height, NSLayoutRelation.Equal, | |
view, NSLayoutAttribute.Height, 1f, view.Frame.Height); | |
return view; | |
} | |
public static UIView ConstrainCenterXToParent(this UIView view, nfloat distance) | |
{ | |
AddConstraint (view, | |
NSLayoutAttribute.CenterX, NSLayoutRelation.Equal, | |
view.Superview, NSLayoutAttribute.CenterX, 1, distance); | |
return view; | |
} | |
public static UIView ConstrainCenterYToParent(this UIView view, nfloat distance) | |
{ | |
AddConstraint (view, | |
NSLayoutAttribute.CenterY, NSLayoutRelation.Equal, | |
view.Superview, NSLayoutAttribute.CenterY, 1, distance); | |
return view; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment