I have the following code to add some toolbar buttons to a NavigationControllers toolbar:
`public override void ViewDidLoad()
{
base.ViewDidLoad();
var browserButton = new UIBarButtonItem("Browser", UIBarButtonItemStyle.Plain, BrowserButtonEventHandler); var backButton = new UIBarButtonItem("Back", UIBarButtonItemStyle.Plain, BackButtonEventHandler); var forwardButton = new UIBarButtonItem("Forward", UIBarButtonItemStyle.Plain, ForwardButtonEventHandler); // Setup toolbar... _navigationController.ToolbarItems = new UIBarButtonItem[] { backButton, forwardButton, browserButton }; _navigationController.Toolbar.BarTintColor = Colors.NavigationBarColor; _navigationController.Toolbar.Translucent = false; _navigationController.ToolbarHidden = false;
}`
The toolbar get displayed fine, but the buttons are not there.
What am I doing wrong ?
Posts
@Hungeber try using SetItems(). Here's an example:
@DannyC that did'nt work either.
I decided to create my own UIToolbar and manually add that to the View. That worked.
@Hungeberg,
Can you share your solution? I am having the same issue.
@John this is what I did to get it working:
`
private void BuildToolBar()
{
var fixedSpace = new UIBarButtonItem(UIBarButtonSystemItem.FixedSpace, null)
{
Width = 26
};
var flexibleSpace = new UIBarButtonItem(UIBarButtonSystemItem.FlexibleSpace, null);
`
I layout the toolbar and other sub views by calling a layout method like so:
`
public override void ViewWillAppear(bool animated)
{
base.ViewWillAppear(animated);
LayoutViews();
}
`
Its important to call the BuildToolBar method after any other sub views are added to the ViewControllers View.
I call BuildToolBar in the ViewControllers constructor like so:
`
public MyViewController()
{
// Add the main sub view here!
// ...
`
@Hungeberg,
Thanks for sharing!
In case you're curious, the solution to the original problem is to set the
ToobarItems
property of the view controller and not theNavigationController
.That worked for me too.
@John
How do you set Toolbar translucency when using this.ToolbarItems ? I see no toolbar property on a UIViewController.
@Hungeberg,
You do everything the same as you did, using the
NavigationController
property to access the actualToolbar
. However, to set the items, you use the view controllersToolbarItems
and not theNavigationController.Toolbar.SetItems
, etc. Hope that helps!@John
Ok, I see.