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