繁体   English   中英

在 Xamarin iOS 13 中自定义导航栏不起作用

[英]Customising navigation bar in Xamarin iOS 13 not working

我正在尝试为 Xamarin 自定义 iOS 13 中的导航栏颜色,但它不起作用我尝试过下面的代码

if (UIDevice.CurrentDevice.CheckSystemVersion(13, 0))          
{            
    var appearance = new UINavigationBarAppearance();
    appearance.ConfigureWithOpaqueBackground();
    appearance.BackgroundColor = Utility.ColorConstants.NavigationBarColor;               
    appearance.TitleTextAttributes = new UIStringAttributes() { ForegroundColor = UIColor.White };
    appearance.LargeTitleTextAttributes = new UIStringAttributes() { ForegroundColor = UIColor.White };

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

    UINavigationBar.Appearance.ScrollEdgeAppearance = appearance;
    UINavigationBar.Appearance.StandardAppearance = appearance;
}

在应用程序委托 class 中,在启动应用程序时,我正在调用上述代码来设置导航栏外观,但它不起作用。

请任何面临这个问题的人都帮助我。

您可以根据需要创建自定义导航栏。

public class BaseViewController: UIViewController
{
  public override void ViewWillAppear(bool animated)
  {
     base.ViewWillAppear(animated);
     NavigationController.NavigationBar.Hidden = true;

     double height = IsiphoneX();

     UIView backView = new UIView()
     {
         BackgroundColor = UIColor.White,
         Frame = new CGRect(0,20,UIScreen.MainScreen.Bounds.Width, height),
     };


     UIButton backBtn = new UIButton() {

         Frame = new CGRect(20, height-44, 40, 44),
         Font = UIFont.SystemFontOfSize(18),

     } ;
     backBtn.SetTitle("Back", UIControlState.Normal);
     backBtn.SetTitleColor(UIColor.Blue, UIControlState.Normal);
     backBtn.AddTarget(this,new Selector("GoBack"),UIControlEvent.TouchUpInside);

     UILabel titleLabel = new UILabel() {
        Frame=new CGRect(UIScreen.MainScreen.Bounds.Width/2-75, 0,150, height),
        Font = UIFont.SystemFontOfSize(20),
        Text = "xxx",
        TextColor = UIColor.Black,
        Lines = 0,

     };

      UILabel line = new UILabel() {

         Frame = new CGRect(0, height, UIScreen.MainScreen.Bounds.Width, 0.5),
         BackgroundColor = UIColor.Black,

      };

      backView.AddSubview(backBtn);
      backView.AddSubview(titleLabel);
      backView.AddSubview(line);

      View.AddSubview(backView);
  }


  double IsiphoneX()
  {

      double height = 44;

      if (UIDevice.CurrentDevice.CheckSystemVersion(11, 0))
      {
         if (UIApplication.SharedApplication.Delegate.GetWindow().SafeAreaInsets.Bottom > 0.0)
          {
             height = 64;
          }
      }

      return height;
   }

  [Export("GoBack")]
  void GoBack()
  {
     NavigationController.PopViewController(true);
  }

  public override void ViewWillDisappear(bool animated)
  {
     base.ViewWillDisappear(animated);

     NavigationController.NavigationBar.Hidden = false;
  }

}

您可以根据需要设置title、backButton 和navigationBar 的属性(例如文本、颜色、背景颜色、字体等)。

另外,由于 iOS 13.0 发布不久,还存在一些问题和新功能。

暂无
暂无

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

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