Xamarin iOS UINavigationBar奇怪的叠加层

问题描述 投票:0回答:1

我正在使用iOS 13在Xamarin 4.5中工作。我正在使用模拟器来运行代码。这是它的外观。

我添加了以下代码来应用红色,但它不会隐藏此白色/灰色叠加层。

UIApplication.SharedApplication.StatusBarStyle = UIStatusBarStyle.LightContent;
UINavigationBar.Appearance.BarTintColor = UIColor.Red;
UINavigationBar.Appearance.TintColor = UIColor.Red;
UINavigationBar.Appearance.TitleTextAttributes = new UIStringAttributes
{
    ForegroundColor = UIColor.Red
};

UINavigationBar.Appearance.BackgroundColor = UIColor.FromRGBA(255, 0, 0, 255);

最后一行添加了红色。

enter image description here

ios iphone xamarin navigation uinavigationcontroller
1个回答
0
投票

这是预期的效果,因为UINavigationBar在iOS 11.0之后具有默认的CALayer。解决方法是,我们可以使用自定义渲染器创建自定义NavigationBar。

在您的iOS项目中]

using Foundation;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;

using xxx;
using xxx.iOS;
using CoreGraphics;
using ObjCRuntime;
[assembly:ExportRenderer(typeof(ContentPage),typeof(MyPageRenderer))]

namespace xxx.iOS
{
    public  class MyPageRenderer:PageRenderer
    {
        public override void ViewWillAppear(bool animated)
        {
            base.ViewWillAppear(animated);



            NavigationController.NavigationBar.Hidden = true;


            double height = IsiphoneX();

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

            };

            // set 
            UIButton backBtn = new UIButton()
            {

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

            };

            backBtn.SetTitle("<",UIControlState.Normal);
            backBtn.SetTitleColor(UIColor.White,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.White,
                Lines = 0,

            };

            UILabel line = new UILabel()
            {

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

            };


            if (NavigationController.ViewControllers.Length > 1)
            {
                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等)

© www.soinside.com 2019 - 2024. All rights reserved.