使用UIToolbar的模态UINavigationController - 工具栏保持为空

时间:2011-01-31 10:32:43

标签: uinavigationcontroller xamarin.ios uitoolbar modalviewcontroller

我正在尝试在模式呈现的UINavigationController的底部放置一个简单的UIToolbar。在此示例中,它应包含两个按钮“cancel”和“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);

...

工具栏显示并具有黑色样式(已分配),但它不包含任何项目。我已经尝试在将hidden设置为false之前分配项目,没有效果。我也尝试使用Toolbar.Items和Toolbar.Hidden以及SetToolbarItems()和SetToolbarHidden()代替,没有运气。

有关此处可能出错的任何提示吗?感谢

修改
网上的大多数样本都会创建自己的UIToolbar并将其添加为子视图。 UINavigationController不需要这样,对吧? AFAICT,它有一个内置的。

1 个答案:

答案 0 :(得分:6)

没关系,我不知道错过了必须以每个子视图为基础提供的工具栏项目。

这有效:

someViewController.ToolbarItems = new UIBarButtonItem[] { cancelButton, flexSpace, someButton };