10

Xamarin Forms – iOS floating tabbar

 3 years ago
source link: https://depblog.weblogs.us/2019/04/02/xamarin-forms-ios-floating-tabbar/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Xamarin Forms – iOS floating tabbar

Sometimes the design of our apps requires us to create custom elements inside of Xamarin Forms.
One thing I wanted to try out, was using a floating tabbar instead of a bottom fixed one.

The end result should be this:

Screenshot-2019-04-02-at-12.17.22.png

Let me show you how you can achieve this in Xamarin Forms targeting iOS.

We actually don’t want to create our own page panel switcher or view host, rather, we would love to reuse the current TabbedPage that is provided by Xamarin Forms. So if we want to alter the one given by Xamarin, we need to create a Custom Renderer for it.
In this custom renderer we will be hide the default TabBar that is provided by iOS and instead we will load a Xamarin Forms ContentView and place it inside the UIView that is currently loaded; placing it somewhere on the bottom of the page.

Meaning we need to convert the ContentView to a iOS native control! This is possible all thanks to Michael Ridland his code shown here https://michaelridland.com/xamarin/creating-native-view-xamarin-forms-viewpage/

The end result Custom Renderer looks like this:

[assembly: ExportRenderer(typeof(CustomTabbedPage), typeof(CustomTabbedPageRenderer))]
namespace XamarinFloatingTabs.iOS.Renderers
    public class CustomTabbedPageRenderer : TabbedRenderer
        private UIView _overlayTabView;
        protected override void OnElementChanged(VisualElementChangedEventArgs e)
            base.OnElementChanged(e);
            Element.PropertyChanged += OnElementPropertyChanged;
            var rect = new CGRect(0, 0, 250, 55);
            _overlayTabView = Helpers.ConvertFormsToNative(new OverlayTabView(), rect);
            var result = UIScreen.MainScreen.Bounds;
            var x = (result.Width / 2) - 125;
            var y = (result.Height - (55 + 20));
            _overlayTabView.Frame = new CGRect(x, y, 250, 55);
        private void OnElementPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
            if (Element is CustomTabbedPage customTabbed)
                if (e.PropertyName.Equals(CustomTabbedPage.IsHiddenProperty.PropertyName))
                    TabBar.Hidden = customTabbed.IsHidden;
                if (e.PropertyName.Equals(CustomTabbedPage.PageTabModeProperty.PropertyName))
                    switch (customTabbed.PageTabMode)
                        case Models.PageTabMode.Floating:
                            View.AddSubview(_overlayTabView);
                            break;
                        case Models.PageTabMode.None:
                            _overlayTabView.RemoveFromSuperview();
                            break;

Now that our view is visible and the default tabbar is hidden, we only need to trigger the actual tab switching.
This is done inside our ContentView!
Inside of the new TabBar view, we have defined 3 buttons. Each button will trigger a page switch, doing this through use of the Xamarin Forms Tabbed Page. We just switch the CurrentPage property of the TabbedPage and this will trigger the normal tab switching.

private void OnDiscoveryClicked(object sender, System.EventArgs e)
    if (App.Current.MainPage is TabbedPage mainPage && sender is Button button)
        mainPage.CurrentPage = mainPage.Children[0];
        Tab1.TextColor = Color.Gray;
        Tab2.TextColor = Tab3.TextColor = Color.DarkGray;

That is basically it!
As you can see the overlay tabbar is just XAML, so you can do whatever you want in it… of you would like to animate the selection or use images, you can just change it.

The end effect is like so:

XFFloatingTabsiOS.gif

As always the code can be found on GitHub here…


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK