Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.