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

标签 ios xamarin.ios ios13

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

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;
}

在启动应用程序时,在应用程序委托(delegate)类中,我调用上面的代码来设置导航栏外观,但它不起作用。

请任何遇到此问题的人帮助我。

最佳答案

您可以根据需要创建自定义navigationBar

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 的属性(如 text 、 color 、BackgroundColor 、font 等)。

此外,由于iOS 13.0发布不久,仍然存在一些现有问题和新功能。

关于ios - 在 Xamarin iOS 13 中自定义导航栏不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58372005/

相关文章:

ios - 核心剧情: axis alternatingBandFills with majorTickLocations inproper display

ios - 在 iOS 5 模拟器中使用 TWeet Compose ViewController 时如何知道设置按钮点击?

ios - iOS/Mac 的地址字段验证

c# - 使用 MonoTouch.Dialog 设置 View 背景颜色

ios - 上传录音到ftp

iOS 13 更新在首次显示时损坏了我的应用程序的键盘

ios - 适用于 iOS 的 Google 云端硬盘实时 API

c# - 更改定时器 iOS Xamarin 的背景

swift - 在 iOS 13 上,生物识别身份验证警报不会显示在旧手机上

swift - 检测 iOS 暗模式更改