在Xamarin中隐藏后退按钮文本

时间:2018-09-18 06:32:40

标签: ios xaml user-interface 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

1 个答案:

答案 0 :(得分:2)

正如here所说,您可以为“后退”按钮标题使用一个空字符串:

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;
  }
相关问题