首页 > 解决方案 > 在 Xamarin 中隐藏后退按钮文本

问题描述

我想在 iOS 上的 Xamarin 中隐藏后退按钮的文本,我尝试过:

<Style TargetType="NavigationPage">
<Setter Property="BackButtonTitle" Value="" />
</Style>

这个代码:

<Style TargetType="ContentPage">
<Setter Property="NavigationPage.BackButtonTitle" Value="" />
</Style>

但文字仍然出现

解决了:

iOS 渲染器:

[assembly: ExportRenderer(typeof(ContentPage), typeof(BackButtonPag))]
[assembly: ExportRenderer(typeof(NavigationPage), typeof(BackButtonNav))]

public class BackButtonPag : PageRenderer
    {
        public override void ViewWillAppear(bool animated)
        {
            base.ViewWillAppear(animated);

            if (NavigationController != null)
                NavigationController.TopViewController.NavigationItem.BackBarButtonItem = new UIBarButtonItem("", UIBarButtonItemStyle.Plain, null);
        }
    }

    public class BackButtonNav : NavigationRenderer
    {
        public override void ViewWillAppear(bool animated)
        {
            base.ViewWillAppear(animated);

            NavigationBar.TopItem.BackBarButtonItem = new UIBarButtonItem("", UIBarButtonItemStyle.Plain, null);
        }
    }

还要加上这个

NavigationPage.BackButtonTitle = ""

到您的 MasterDetailPage 的 Main.xaml

标签: iosxamluser-interfacexamarin

解决方案


正如这里所说,您可以使用空字符串作为后退按钮标题:

NavigationPage.SetBackButtonTitle(this, "");

或者您可以将标题设置UIBarButtonItem为清晰

public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
  {
     // . . .
     var attribute = new UITextAttributes();
     attribute.TextColor = UIColor.Clear;
     UIBarButtonItem.Appearance.SetTitleTextAttributes(attribute, UIControlState.Normal);
     UIBarButtonItem.Appearance.SetTitleTextAttributes(attribute, UIControlState.Highlighted);
     //. . .

     return true;
  }

推荐阅读