我正在尝试在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;
}
在启动应用程序时,在应用程序委托类中,我正在调用上面的代码来设置导航栏外观,但是它不起作用。
请遇到这个问题的任何人都可以帮助我。
答案 0 :(得分:0)
您可以根据需要创建自定义的 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是不久前发布的,因此仍然存在一些现有问题和新功能。