提问人:Waleed 提问时间:4/20/2020 最后编辑:Waleed 更新时间:4/20/2020 访问量:121
Xamarin iOS UINavigationBar 奇怪的覆盖
Xamarin iOS UINavigationBar weird overlay
问:
我正在使用 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);
最后一行添加了红色。
答:
0赞
Lucas Zhang
4/20/2020
#1
这将是预期的效果,因为 UINavigationBar 在 iOS 11.0 之后具有默认值。作为解决方法,我们可以使用自定义渲染器创建自定义 NavigationBarCALayer
在 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 等)
评论