-1

I dont have Titles and i prefer to use only Icons but the Icons are not centered since the Title's space and padding still appear, any workaround ?Picture of the problem

<Shell xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:local="clr-namespace:WebViewSample"
             mc:Ignorable="d"
             x:Class="WebViewSample.MainPage"
             Shell.NavBarIsVisible="False"       
            
       >
 
        <TabBar>
        <Tab Icon="home.png">
           
            <ShellContent>
                <local:HomePage/>
            </ShellContent>
        </Tab>
        <Tab Icon="user.png">
            <ShellContent>
                <local:MyAccount/>
            </ShellContent>
        </Tab>
        <Tab Icon="cart.png">
            <ShellContent>
                <local:OrderPage/>
            </ShellContent>
        </Tab>
        <Tab Icon="search.png" Title="" >
            <ShellContent >
                <local:BarCodeScanner/>
            </ShellContent>
        </Tab>
    </TabBar>
</Shell>

1 Answers1

3

We could implement it by using Custom Renderer

in iOS

AppShell here is the Shell in my project . you need to modify it as you need(seems in your sample is MainPage)

using xxx;
using xxx.iOS;
using Foundation;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;

[assembly: ExportRenderer(typeof(AppShell), typeof(MyShellRenderer))]
namespace xxx.iOS
{
    public class MyShellRenderer : ShellRenderer
    {
        protected override IShellSectionRenderer CreateShellSectionRenderer(ShellSection shellSection)
        {
            var renderer = base.CreateShellSectionRenderer(shellSection);
            if (renderer != null)
            {

            }
            return renderer;
        }

        protected override IShellTabBarAppearanceTracker CreateTabBarAppearanceTracker()
        {
            return new CustomTabbarAppearance();
        }
    }

    public class CustomTabbarAppearance : IShellTabBarAppearanceTracker
    {
        public void Dispose()
        {

        }

        public void ResetAppearance(UITabBarController controller)
        {

        }

        public void SetAppearance(UITabBarController controller, ShellAppearance appearance)
        {
            UITabBar myTabBar = controller.TabBar;

            if (myTabBar.Items != null)
            {
                foreach (UITabBarItem item in myTabBar.Items)
                {
                    item.Title = null;
                    item.ImageInsets = new UIEdgeInsets(10, 0, 0, 0);
                }
                //The same logic if you have itemThree, itemFour....
            }

            

        }

        public void UpdateLayout(UITabBarController controller)
        {

        }
    }
}

in Android

using Android.App;

using Android.Content;

using Android.Support.Design.BottomNavigation;
using Android.Support.Design.Widget;
using Android.Views;

using Android.Widget;

using App13;

using App13.Droid;
using System;
using Xamarin.Forms;

using Xamarin.Forms.Platform.Android;

[assembly: ExportRenderer(typeof(AppShell), typeof(ShellCustomRenderer))]
namespace App13.Droid

{

    public class ShellCustomRenderer : ShellRenderer

    {

        public ShellCustomRenderer(Context context) : base(context)

        {



        }


        protected override IShellBottomNavViewAppearanceTracker CreateBottomNavViewAppearanceTracker(ShellItem shellItem)

        {

            return new MarginedTabBarAppearance();

        }


    }



    public class ToolbarAppearance : IShellToolbarAppearanceTracker

    {

        public void Dispose()

        {



        }

        public void ResetAppearance(Android.Support.V7.Widget.Toolbar toolbar, IShellToolbarTracker toolbarTracker)
        {
            throw new NotImplementedException();
        }

        public void SetAppearance(Android.Support.V7.Widget.Toolbar toolbar, IShellToolbarTracker toolbarTracker, ShellAppearance appearance)
        {
            throw new NotImplementedException();
        }
    }



   

    public class MarginedTabBarAppearance : IShellBottomNavViewAppearanceTracker

    {

        public void Dispose()

        {

        }



        public void ResetAppearance(BottomNavigationView bottomView)

        {

         
        }

      
        public void SetAppearance(BottomNavigationView bottomView, IShellAppearanceElement appearance)
        {
            bottomView.LabelVisibilityMode = LabelVisibilityMode.LabelVisibilityUnlabeled; 
        }
    }

}

Now the icon are in the center of tab .

enter image description here

Lucas Zhang
  • 18,630
  • 3
  • 12
  • 22
  • where do i put this code do i create a custom renderer ? – Nevergrim Aug 12 '20 at 08:55
  • Both on iOS and Android Project . I uploaded the sample https://github.com/luczha/ShellSample you could have a refer . – Lucas Zhang Aug 12 '20 at 09:12
  • If the above answer id helpful , you could accept it as answer https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work . – Lucas Zhang Aug 12 '20 at 09:13
  • What i am missing ? I did the same but i am getting an error MarginedTabBarAppearence does not implement ISellBottomNavViewAppearanceTracker – Nevergrim Aug 12 '20 at 09:32
  • Update the version of XF to latest version both in share project and iOS , Android project . – Lucas Zhang Aug 12 '20 at 09:34
  • System.InvalidCastException Message=Specified cast is not valid. now got this exception with the error – Nevergrim Aug 12 '20 at 10:54
  • Are you new to xamarin? You could share your sample – Lucas Zhang Aug 12 '20 at 10:59
  • ``` using Xamarin.Forms; using Xamarin.Forms.Platform.Android; [assembly: ExportRenderer(typeof(HomePage), typeof(ShellCustomRenderer))] namespace WebViewSample.Droid { public class ShellCustomRenderer : ShellRenderer { public ShellCustomRenderer(Context context) : base(context) ``` – Nevergrim Aug 12 '20 at 11:08
  • You could accept it as answer if the reply is helpful(click the “✔” in the upper left corner of this answer), it will help others who have similar issue . Then post the full sample so that I can test it on my side . – Lucas Zhang Aug 18 '20 at 10:48
  • I'd only give a small piece of advice to add this in Android: bottomView.ItemIconSize = 96. It looks better. – Federico Navarrete Jun 12 '21 at 12:02