Samantha J T Star
Samantha J T Star

Reputation: 32838

Removing navigation bar line in Xamarin.iOS not working

I have a Xamarin.Forms application and currently working on removing the line under the navigation bar on iOS.

public partial class MainPage : TabbedPage
{
   public MainPage()
   {
      InitializeComponent();
      var homePage = new NavigationPage(new HomePage())
      {
         Title = "Home",
         Icon = "home.png"
      };
      var helpPage = new NavigationPage(new HelpPage())
      {
         Title = "Help",
         Icon = "help.png"
      };
      // other declarations here

      Children.Add(homePage);
      Children.Add(helpPage);
      // and more
   }
}

I have tried UINavigationBar.Appearance.ShadowImage = new UIImage(); in the AppDelegate but it's not working at all so I created the following renderer.

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

[assembly: ExportRenderer(typeof(NavigationPage), typeof(Japanese.iOS.NavigationPageRenderer))]
namespace Japanese.iOS
{
    public class NavigationPageRenderer : NavigationRenderer
    {
        public override void ViewDidLoad()
        {
            base.ViewDidLoad();
            UINavigationBar.Appearance.SetBackgroundImage(new UIImage(), UIBarMetrics.Default);
            UINavigationBar.Appearance.ShadowImage = new UIImage();
        }
    }
}

This works but only in the HomePage which is the first page that would open when the application starts.

enter image description here As the image below shows the HelpPage still has the line showing as well as the rest of my pages.

enter image description here Anyone has any idea what I am doing wrong?

Upvotes: 2

Views: 1367

Answers (1)

Ranga
Ranga

Reputation: 1108

You have to set the Appearance and ShadowImage on the NavigationBar instance in the PageRenderer

public class NavigationPageRenderer : NavigationRenderer
{
    protected override void OnElementChanged(VisualElementChangedEventArgs e)
    {
        base.OnElementChanged(e);
        NavigationBar.SetBackgroundImage(new UIImage(), UIBarMetrics.Default);
        NavigationBar.ShadowImage = new UIImage();
    }
}

Xamarin already provides property to hide the Separator through Platform Sepcifics - https://learn.microsoft.com/en-us/xamarin/xamarin-forms/platform/platform-specifics/consuming/ios#navigationpage-hideseparatorbar

Upvotes: 1

Related Questions