[英]How to Change Navigation Bar Style in IOS Xamarin.Forms
我引用了以下鏈接來全局隱藏導航欄的后退按鈕標題及其工作正常,但是,當我向工具欄添加任何文本時,工具欄上的文本也會被隱藏。 任何人都可以提出解決此問題的建議。
這是我的工具欄代碼:
ToolbarItem tbi = new ToolbarItem() { Text = "Text", Order = ToolbarItemOrder.Primary };
ToolbarItems.Add(tbi);
更改或刪除Xamarin.Forms上的后退按鈕文本的最簡單方法是使用以下代碼:
NavigationPage.SetBackButtonTitle(this, "");
請注意,您需要在頁面上執行此操作, 然后頁面上的“后退”按鈕即出現在調用頁面上。
我試過了,您的工具欄代碼正常工作。
編輯
您還可以創建包含隱藏工具欄按鈕代碼的基礎頁面,並從中繼承所有頁面。
這是基本頁面:
public class BasePage : ContentPage
{
public BasePage ()
{
NavigationPage.SetBackButtonTitle(this, "");
}
}
然后繼承的頁面看起來像這樣
public partial class MainPage : BasePage
{
public MainPage()
{
InitializeComponent();
}
}
如果它是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>
我們可以看到,在iOS平台上進行編譯時, ToolbarItem
將呈現為UIBarButtonItem
。
因此,以下代碼也將影響ToolbarItem
外觀。
UITextAttributes attribute = new UITextAttributes {TextColor = UIColor.Clear };
UIBarButtonItem.Appearance.SetTitleTextAttributes(attribute, UIControlState.Normal);
這是使用SetBackButtonTitlePositionAdjustment
的最簡單方法。
UIBarButtonItem.Appearance.SetBackButtonTitlePositionAdjustment (new UIOffset (-100, 0), UIBarMetrics.Default);
如果您仍然想找到其他方法,請嘗試使用自定義渲染器 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.