Created
December 5, 2014 05:17
-
-
Save adrianstevens/b5b6c177110a6cf0ebdb to your computer and use it in GitHub Desktop.
Xamarin Visual Format Language Example for iOS Auto Layouts
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
using System; | |
using MonoTouch.UIKit; | |
using MonoTouch.CoreGraphics; | |
using MonoTouch.Foundation; | |
namespace Code | |
{ | |
public class MyViewController : UIViewController | |
{ | |
UIButton button1, button2, button3, button4; | |
public MyViewController () | |
{ | |
} | |
public override void ViewDidLoad () | |
{ | |
base.ViewDidLoad (); | |
this.View.BackgroundColor = UIColor.White; | |
CreateButtons (); | |
VisualFormat (); | |
} | |
UIButton CreateButton (string title) | |
{ | |
var button = UIButton.FromType (UIButtonType.RoundedRect); | |
button.SetTitle (title, UIControlState.Normal); | |
button.Layer.BorderWidth = 1; | |
button.TranslatesAutoresizingMaskIntoConstraints = false; | |
return button; | |
} | |
void CreateButtons () | |
{ | |
button1 = CreateButton ("Button1"); | |
button2 = CreateButton ("Button2"); | |
button3 = CreateButton ("Button3"); | |
button4 = CreateButton ("Button4"); | |
this.View.Add (button1); | |
this.View.Add (button2); | |
this.View.Add (button3); | |
this.View.Add (button4); | |
} | |
void VisualFormat () | |
{ | |
var keys = new object [] { "button1", "button2", "button3", "button4" }; | |
var objects = new object [] { button1, button2, button3, button4 }; | |
var myViews = NSDictionary.FromObjectsAndKeys (objects, keys); | |
var height = new NSDictionary ("height", 50.0); | |
//horizontal layout | |
this.View.AddConstraints( NSLayoutConstraint.FromVisualFormat(@"|-[button1(button2)]-[button2(button3)]-[button3(button4)]-[button4]-|", | |
NSLayoutFormatOptions.AlignAllTop | NSLayoutFormatOptions.AlignAllBottom, height, myViews)); | |
//vertical layout | |
this.View.AddConstraints(NSLayoutConstraint.FromVisualFormat(@"V:[button1(height)]-|", | |
0, height, myViews)); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment