Skip to content

Instantly share code, notes, and snippets.

@yetanotherchris
Created February 15, 2013 11:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yetanotherchris/4959932 to your computer and use it in GitHub Desktop.
Save yetanotherchris/4959932 to your computer and use it in GitHub Desktop.
Monotouch: Adding controls (Views) to the first controller
public class HomeViewController : UIViewController
{
public override void ViewDidLoad()
{
Title = "Home";
// A 'view' aka label control for this view
UILabel label = new UILabel();
label.Text = "HomeViewController";
label.Frame = Center(100, 100);
label.BackgroundColor = UIColor.LightGray;
View.AddSubview(label);
// 2 toolbar items
UIBarButtonItem item1 = new UIBarButtonItem();
item1.Title = "Click me";
item1.Clicked += delegate(object sender, EventArgs e)
{
Level2ViewController controller = new Level2ViewController();
NavigationController.PushViewController(controller, true);
};
UIBarButtonItem item2 = new UIBarButtonItem();
item2.Title = "View with no back";
item2.Clicked += delegate(object sender, EventArgs e)
{
ViewWithNoBackController controller = new ViewWithNoBackController();
NavigationController.PushViewController(controller, true);
};
ToolbarItems = new UIBarButtonItem[] { item1, item2 };
base.ViewDidLoad();
}
public override void ViewWillAppear(bool animated)
{
// Re-show the toolbar here for consistency
NavigationController.SetToolbarHidden(false, true);
base.ViewWillAppear(animated);
}
public RectangleF Center(float width, float height)
{
var rect = new RectangleF((View.Frame.Width / 2) - (width / 2), (View.Frame.Height / 2) - (height / 2), width, height);
// Prints:
// Rect: {X=110,Y=180,Width=100,Height=100}
// Frame: {X=0,Y=20,Width=320,Height=460}
Console.WriteLine("Rect: {0}", rect);
Console.WriteLine("Frame: {0}", View.Frame);
return rect;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment