Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modal UINavigationController with UIToolbar - Toolbar remains empty

I'm trying to put a simple UIToolbar at the bottom of a modally presented UINavigationController. In this sample, it should contain two buttons "cancel" and "something".

...

UINavigationController modalNavigationController = new UINavigationController(someViewController);
modalNavigationController.NavigationBar.BarStyle = UIBarStyle.Black;
modalNavigationController.Toolbar.BarStyle = UIBarStyle.Black;
modalNavigationController.ToolbarHidden = false;

UIBarButtonItem cancelButton = new UIBarButtonItem("cancel", UIBarButtonItemStyle.Plain, delegate {
  modalNavigationController.DismissModalViewControllerAnimated(true);
});
UIBarButtonItem flexSpace = new UIBarButtonItem(UIBarButtonSystemItem.FlexibleSpace, null);
UIBarButtonItem someButton = new UIBarButtonItem("something", UIBarButtonItemStyle.Plain, delegate {
  Console.WriteLine("here we are!");
});
modalNavigationController.ToolbarItems = new UIBarButtonItem[] { cancelButton, flexSpace, someButton };

parentController.PresentModalViewController(modalNavigationController, true);

...

The toolbar appears and has black style (as assigned), it does not contain any items though. I've tried assigning the items before setting hidden to false, no effect. I also tried using Toolbar.Items and Toolbar.Hidden as well as SetToolbarItems() and SetToolbarHidden() instead, with no luck.

Any hints on what might be wrong here? Thanks

EDIT:
Most samples on the net create their own UIToolbar and add it as a subview. That is not required with a UINavigationController, right? AFAICT, it has one built-in.

like image 602
riha Avatar asked Jan 31 '11 10:01

riha


1 Answers

Nevermind, I somehow missed that toolbar items have to be supplied on a per-subview basis.

This works:

someViewController.ToolbarItems = new UIBarButtonItem[] { cancelButton, flexSpace, someButton };
like image 141
riha Avatar answered Oct 11 '22 16:10

riha