Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 91519

How to transform secondary ToolbarItems to UIBarButtonItems in the bottom toolbar on iOS?

$
0
0

First of all, I think the ToolbarItems in Forms are the right approach to have a generic abstraction to the platform specific actions users can execute on each platform. Furthermore, the Android implementation today looks fine to me. The iOS implementation though renders the bar right beneath the UINavigationBar of the UINavigationController and not (as it is common sense on iOS) at the bottom of the screen.

So I tried to transform the secondary navigation items into UIBarButtonItems before rendering the screen and then place them the UIToolbar of the NavigationController my view stack is embedded in.

Unfortunately I was not able to have it render properly so far. The toolbar seems to be rendered fullscreen and even manually changing its frame does not have an impact on its size.

Here is my current solution apporach:

The page, which has secondary Toolbaritems (and hence should display a UIToolbar) inherits from a ToolbarAwarePage for which I export a custom renderer on iOS. The listing shows an attempt using viewWillAppear as viewDidLoad is too early to access the NavigationController (still null).

`

public class ToolbarAwareRenderer : PageRenderer
{
    public override void ViewWillAppear (bool animated)
    {

        base.ViewWillAppear (animated);

        // remove the toolbar items from the page
        var page = App.NavigationPage.CurrentPage;
        var secondaryToolbarItems = new List<ToolbarItem> (page.ToolbarItems.Where (item => item.Order == ToolbarItemOrder.Secondary));
        foreach (var toolbarItem in secondaryToolbarItems) {
            page.ToolbarItems.Remove (toolbarItem);
        }

        var formsToolBarItemArray = secondaryToolbarItems.ToArray ();
        var numberOfItems = formsToolBarItemArray.Length;

        if (numberOfItems != 0) {
            var nativeToolBarItemArray = new UIBarButtonItem[numberOfItems];
            // Add each secondary toolbaritem as a UIBarButtonItem to the UIToolBar of the underlying UINavigationController

            for (var itemIndex = 0; itemIndex < numberOfItems; itemIndex = itemIndex + 1) {
                UIBarButtonItem barButtonItem = formsToolBarItemArray [itemIndex].ToUIBarButtonItem ();
                nativeToolBarItemArray [itemIndex] = barButtonItem;
            }

            SetToolbarItems (nativeToolBarItemArray, false);
            NavigationController.SetToolbarHidden (false, false);

        }
    }
}

`

I also tried to do this transformation in a custom renderer for the NavigationPage itself (using the OnPushAsync(...) and PushViewController(...) methods), but the result was similar to the one of the approach here.

In general, I think that the Xamarin Team has forseen somethink like this because there is the public ToolbarItem.ToUIBarButtonItem() method, which I use.

Any help or suggestions are appreciated.

Thanks,
Sven


Viewing all articles
Browse latest Browse all 91519

Trending Articles