Xamarin iOS UINavigationBar странное наложение - PullRequest
0 голосов
/ 20 апреля 2020

Я работаю в Xamarin 4.5 с iOS 13. Я использую Simulator для запуска кода. Вот как это выглядит.

Я добавил следующий код, чтобы применить красный цвет, но он не скрывает это белое / серое наложение.

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

1 Ответ

0 голосов
/ 20 апреля 2020

Это будет ожидаемый эффект, потому что UINavigationBar имеет значение по умолчанию CALayer после iOS 11.0. В качестве обходного пути, мы могли бы создать пользовательский 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, как вам нужно ( например, текст, цвет, фоновый цвет, шрифт, например)

...