0

Above is my tabbar code I want to set the width of top tabbar items so that it fits to screen

enter image description here

I am using 3 toptabs inside 1st bottom tab, so as the bottoms tabs are fit to screen and equally spaced, i want the same for top tabs as well in IOS

1 Answer 1

0

To fit the top three tabbar items to the width of the screen, you can use Handlers in MAUI.

Here're the detailed steps on how to implement it:

  1. Under folder Platform-->iOS, create the class: MyiOSCustomShellRenderer(please delete the default namespace yourproject.Platforms.iOS) like below:
public class MyiOSCustomShellRenderer : ShellRenderer
{

     protected override IShellSectionRenderer CreateShellSectionRenderer(ShellSection shellSection)
     {
         var shellSectionRenderer = new CustomShellSectionRenderer(this);
         return shellSectionRenderer;

      }
}

public class CustomShellSectionRenderer : ShellSectionRenderer
{
        public CustomShellSectionRenderer(IShellContext context) : base(context){ }
        protected override IShellSectionRootRenderer CreateShellSectionRootRenderer(ShellSection shellSection, IShellContext shellContext)
        {
            var renderer = new CustomShellSectionRootRenderer(shellSection, shellContext);
            return renderer;
        }
}

public class CustomShellSectionRootRenderer : ShellSectionRootRenderer
{
        public CustomShellSectionRootRenderer(ShellSection section, IShellContext context) : base(section, context){ }
        protected override IShellSectionRootHeader CreateShellSectionRootHeader(IShellContext shellContext)
        {
           var renderer = new CustomShellSectionRootHeader(shellContext);
            return renderer;
        }
    }
    public class CustomShellSectionRootHeader : ShellSectionRootHeader
    {
        public CustomShellSectionRootHeader(IShellContext context) : base(context)
    {}
        public override UICollectionViewCell GetCell(UICollectionView collectionView, NSIndexPath indexPath)
        {

            var cell = base.GetCell(collectionView, indexPath) as ShellSectionHeaderCell;

            var layout = new UICollectionViewFlowLayout();
            layout.MinimumInteritemSpacing = UIScreen.MainScreen.Bounds.Size.Width / 5;
            layout.SectionInset = new UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 0);
            layout.ItemSize = new SizeF(120, 50);
            layout.ScrollDirection = UICollectionViewScrollDirection.Horizontal;
            collectionView.CollectionViewLayout = layout;
            return cell;
        }
    }

    public class CustomUICollectionViewFlowLayout : UICollectionViewFlowLayout
    {
        public override UICollectionViewLayoutAttributes[] LayoutAttributesForElementsInRect(CGRect rect)
        {
            return base.LayoutAttributesForElementsInRect(rect);
        }
    }

  1. Register the handlers in the MauiProgram.cs:
builder.ConfigureMauiHandlers(h =>
{
#if IOS
       h.AddHandler<Shell, MyiOSCustomShellRenderer>();
#endif
});

Sign up to request clarification or add additional context in comments.

2 Comments

it is working, also I want to set the background color as transparent for the top bar can you give me answer to that
@MeghnaSingh You can set it via Shell.BackgroundColor="Transparent" in your AppShell.xaml, see learn.microsoft.com/en-us/dotnet/maui/fundamentals/shell/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.