简体   繁体   中英

How to Change Navigation Bar Style in IOS Xamarin.Forms

I have referred the below link to hide the navigation bar back button title globally and its working fine, But when I add any text to toolbar, the toolbar text also hidden. Can anyone give suggestion to fix this issue.

How to Hide Navigation bar back button title globally in xamarin.ios

Here is my toolbar code:

ToolbarItem tbi = new ToolbarItem() { Text = "Text", Order = ToolbarItemOrder.Primary };
ToolbarItems.Add(tbi);

The easiest way to change or remove the back button text on Xamarin.Forms is with this code:

NavigationPage.SetBackButtonTitle(this, "");

Note you need to do this in the page before the page the back button appears on ie the calling page.

I tried this and your toolbar code worked correctly.

EDIT

You can also create a base page containing the hide toolbar button code and inherit all pages from that.

Here is the base page:

public class BasePage : ContentPage
{
    public BasePage ()
    {
        NavigationPage.SetBackButtonTitle(this, "");
    }
}

Then the inherited page looks like this

public partial class MainPage : BasePage
{
    public MainPage()
    {
        InitializeComponent();
    }
}

and if it is a XAML page ensure you change the page type

<pages:BasePage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:pages="clr-namespace:ScratchPad.Pages;assembly=ScratchPad"
             x:Class="ScratchPad.Pages.MainPage"
             Title="List Test">
    <StackLayout>
        <Button Text="Page Two" Command="{Binding PageTwoCommand}"/>
        <ListView ItemsSource="{Binding Items}"
                  SelectedItem="{Binding SelectedItem, Mode=TwoWay}">
        </ListView>

    </StackLayout>

</pages:BasePage>

在此处输入图片说明

Refer to ToolbarItem in Official Source Code

We can see here ToolbarItem will be rendered into UIBarButtonItem when compiling on iOS platform.

So the following code will also affect ToolbarItem appearance.

UITextAttributes attribute = new UITextAttributes {TextColor = UIColor.Clear };
UIBarButtonItem.Appearance.SetTitleTextAttributes(attribute, UIControlState.Normal);

It is the easiest way to use SetBackButtonTitlePositionAdjustment .

UIBarButtonItem.Appearance.SetBackButtonTitlePositionAdjustment (new UIOffset (-100, 0), UIBarMetrics.Default);

If you still want to find another way, try Custom Renderers .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM