Skip to content

Instantly share code, notes, and snippets.

@praeclarum
Created March 18, 2013 23:33
Show Gist options
  • Save praeclarum/5192008 to your computer and use it in GitHub Desktop.
Save praeclarum/5192008 to your computer and use it in GitHub Desktop.
using System;
using MonoTouch.UIKit;
using System.Drawing;
namespace Async.iOS
{
public class LayoutViewController : UIViewController
{
UITextField text;
UIButton button;
const float ButtonWidth = 88;
const float HPadding = 22;
const float VPadding = 44;
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
View.BackgroundColor = UIColor.White;
text = new UITextField {
Placeholder = "Search",
BorderStyle = UITextBorderStyle.RoundedRect,
};
button = UIButton.FromType (UIButtonType.RoundedRect);
button.SetTitle ("Search", UIControlState.Normal);
View.AddSubviews (text, button);
// LayoutWithSpringsAndStruts ();
// LayoutWithConstraints ();
LayoutWithEase ();
}
void LayoutWithSpringsAndStruts ()
{
var b = View.Bounds;
button.Frame = new RectangleF (
b.Width - HPadding - ButtonWidth,
VPadding,
ButtonWidth,
44);
button.AutoresizingMask =
UIViewAutoresizing.FlexibleLeftMargin |
UIViewAutoresizing.FlexibleBottomMargin;
text.Frame = new RectangleF (
HPadding,
VPadding,
b.Width - ButtonWidth - 3 * HPadding,
text.Font.PointSize + 14);
text.AutoresizingMask =
UIViewAutoresizing.FlexibleWidth |
UIViewAutoresizing.FlexibleBottomMargin;
}
void LayoutWithConstraints ()
{
button.TranslatesAutoresizingMaskIntoConstraints = false;
// Set button width
View.AddConstraint (NSLayoutConstraint.Create (
button, NSLayoutAttribute.Width,
NSLayoutRelation.Equal,
null, NSLayoutAttribute.NoAttribute,
0, ButtonWidth));
// Set button top
View.AddConstraint (NSLayoutConstraint.Create (
button, NSLayoutAttribute.Top,
NSLayoutRelation.Equal,
View, NSLayoutAttribute.Top,
1, VPadding));
// Set button right
View.AddConstraint (NSLayoutConstraint.Create (
button, NSLayoutAttribute.Right,
NSLayoutRelation.Equal,
View, NSLayoutAttribute.Right,
1, -HPadding));
text.TranslatesAutoresizingMaskIntoConstraints = false;
// Set text left
View.AddConstraint (NSLayoutConstraint.Create (
text, NSLayoutAttribute.Left,
NSLayoutRelation.Equal,
View, NSLayoutAttribute.Left,
1, HPadding));
// Set text right
View.AddConstraint (NSLayoutConstraint.Create (
text, NSLayoutAttribute.Right,
NSLayoutRelation.Equal,
button, NSLayoutAttribute.Left,
1, -HPadding));
// Set text top
View.AddConstraint (NSLayoutConstraint.Create (
text, NSLayoutAttribute.Top,
NSLayoutRelation.Equal,
button, NSLayoutAttribute.Top,
1, 0));
}
void LayoutWithEase ()
{
View.ConstrainLayout (() =>
button.Frame.Width == ButtonWidth &&
button.Frame.Right == View.Frame.Right - HPadding &&
button.Frame.Top == View.Frame.Top + VPadding &&
text.Frame.Left == View.Frame.Left + HPadding &&
text.Frame.Right == button.Frame.Left - HPadding &&
text.Frame.Top == button.Frame.Top
);
}
}
}
@ChuckPinkert
Copy link

Do you know why this sample viewController would stop working in a Nav controller? It I chose the springs and struts it works perfectly, if I do the auto-layout the background goes black and the constraints related to the superview fail.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment