Forum Xamarin.iOS

UIToolBar buttons not being shown

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

  • DannyCDannyC US ✭✭✭✭
    edited October 2013

    @Hungeber try using SetItems(). Here's an example:

    _navigationController.SetItems(new UIBarButtonItem[] { backButton, forwardButton, browserButton }, true);
    
  • PerHungebergMllerPerHungebergMller DKMember ✭✭✭

    @DannyC that did'nt work either.

    I decided to create my own UIToolbar and manually add that to the View. That worked.

  • JohnMillerJohnMiller USForum Administrator, Xamarin Team Xamurai

    @Hungeberg,

    Can you share your solution? I am having the same issue.

  • PerHungebergMllerPerHungebergMller DKMember ✭✭✭

    @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);

            _shareButton = new UIBarButtonItem(UIBarButtonSystemItem.Action, (sender, args) =>
                {
                    // I am currently working on this...
                });
    
            _backButton = new UIBarButtonItem(Resources.BackButton, UIBarButtonItemStyle.Plain, (sender, args) => _web.GoBack());
            _forwardButton = new UIBarButtonItem(Resources.ForwardButton, UIBarButtonItemStyle.Plain, (sender, args) => _web.GoForward());
    
            _toolbar =
                new UIToolbar()
                    {
                        Translucent = false,
                        Items = new UIBarButtonItem[]
                            {
                                _backButton,
                                fixedSpace,
                                _forwardButton,
                                flexibleSpace,
                                _shareButton
                            }
                    };
    
            _backButton.Enabled = false;
            _forwardButton.Enabled = false;
            View.AddSubview(_toolbar);
        }
    

    `

    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();
    }

        public override void DidRotate(UIInterfaceOrientation fromInterfaceOrientation)
        {
            base.DidRotate(fromInterfaceOrientation);
            LayoutViews();
        }
    
        private void LayoutViews()
        {
            _toolbar.Frame = new RectangleF(0, View.Bounds.Height - ViewHelper.GetDefaultNavigationBarHeight(),
                                            View.Bounds.Width, ViewHelper.GetDefaultNavigationBarHeight());
    
            // You can layout any other sub views here...
        }
    

    `

    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!
    // ...

            BuildToolBar();
        }
    

    `

  • JohnMillerJohnMiller USForum Administrator, Xamarin Team Xamurai

    @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 the NavigationController.

    this.NavigationController.ToolbarHidden = false;
    this.ToolbarItems = new UIBarButtonItem[] { button };
    

    That worked for me too.

  • PerHungebergMllerPerHungebergMller DKMember ✭✭✭

    @John

    How do you set Toolbar translucency when using this.ToolbarItems ? I see no toolbar property on a UIViewController.

  • JohnMillerJohnMiller USForum Administrator, Xamarin Team Xamurai
    edited October 2013

    @Hungeberg,

    You do everything the same as you did, using the NavigationController property to access the actual Toolbar. However, to set the items, you use the view controllers ToolbarItems and not the NavigationController.Toolbar.SetItems, etc. Hope that helps!

  • PerHungebergMllerPerHungebergMller DKMember ✭✭✭

    @John

    Ok, I see.

Sign In or Register to comment.