【问题标题】:Customising navigation bar in Xamarin iOS 13 not working在 Xamarin iOS 13 中自定义导航栏不起作用
【发布时间】:2019-10-14 07:33:08
【问题描述】:

我正在尝试在 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;
}

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

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

【问题讨论】:

    标签: ios xamarin.ios ios13


    【解决方案1】:

    您可以根据需要创建自定义 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 e.g.)。

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

    【讨论】:

    • 张这段代码我可以用于任何视图控制器类,但是我在启动应用程序时设置在应用程序委托类。假设我有登录屏幕并且在单击登录按钮时显示活动指示器为用于显示加载消息的 UIView 全屏在这种情况下如何更改导航栏的颜色。
    • 在 iOS 13 之前我使用的是这段代码 { UINavigationBar.Appearance.BarTintColor = Utility.ColorConstants.NavigationBarColor; UINavigationBar.Appearance.TintColor = UIColor.White; UINavigationBar.Appearance.SetTitleTextAttributes(new UITextAttributes { TextColor = UIColor.White }); UIBarButtonItem.Appearance.SetTitleTextAttributes(new UITextAttributes { TextColor = UIColor.White }, UIControlState.Normal);
    • 编辑关于登录逻辑的问题。不是自定义导航栏。
    猜你喜欢
    • 2012-01-05
    • 1970-01-01
    • 2013-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多