简体   繁体   English

如何在IOS Xamarin.Forms中更改导航栏样式

[英]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 如何在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: 更改或删除Xamarin.Forms上的后退按钮文本的最简单方法是使用以下代码:

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 如果它是XAML页面,请确保更改页面类型

<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 请参阅官方源代码中的ToolbarItem

We can see here ToolbarItem will be rendered into UIBarButtonItem when compiling on iOS platform. 我们可以看到,在iOS平台上进行编译时, ToolbarItem将呈现为UIBarButtonItem

So the following code will also affect ToolbarItem appearance. 因此,以下代码也将影响ToolbarItem外观。

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

It is the easiest way to use SetBackButtonTitlePositionAdjustment . 这是使用SetBackButtonTitlePositionAdjustment的最简单方法。

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

If you still want to find another way, try Custom Renderers . 如果您仍然想找到其他方法,请尝试使用自定义渲染器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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