Xamarin Forms iOS TitleBarTextColor not changing



到目前为止,为了更改标题栏文本颜色,我尝试了很多,我的代码现在确实更改了后退按钮颜色和屏幕顶部的区域,但是标题!

在我的 AppDelegate DoneLaunch 函数中(它在 Forms.Init(( 之后和 LoadApplication(( 之前(:

UINavigationBar.Appearance.SetTitleTextAttributes(new UITextAttributes
{
TextColor = UIColor.White
});

在我的视图中模型:

(App.Current.MainPage as NavigationPage).BarBackgroundColor = Color.FromHex("#990000");
(App.Current.MainPage as NavigationPage).BarTextColor = Color.White;

这就是我在页面(不是所有页面(之间导航的方式:

await _navigationService.NavigateAsync(new Uri("http://wwww.x.com/NavigationPage/TabbedNavigationPage?selectedTab=XPage/Document", UriKind.Absolute));

我什至尝试在棱镜导航后调用 ViewModel 代码,但它不起作用......我是初学者,不完全理解棱镜和/或 Xamarin 表单。

[编辑] -> 我还尝试创建一个新类并从NavigationPage继承它,在其构造函数中设置BarTextColor并在导航中使用该类,如下所示:await _navigationService.NavigateAsync(new Uri("http://wwww.x.com/NEWCLASSCREATED/TabbedNavigationPage?selectedTab=XPage/Document", UriKind.Absolute));但是,如您所知,它仍然无法正常工作。

这是一个图像;)

图像看我不是说谎

感谢您的支持!

所以我最终设法解决了这个问题......

我必须做的是创建自定义Content Page因为所有其他解决方案都不起作用。所以我只在我的 iOS 项目中创建了这个自定义渲染器:

[assembly: ExportRenderer(typeof(ContentPage), typeof(CustomContentPageRenderer))]
namespace TestProject.iOS.Bll.Utils.Renderers
{
public class CustomContentPageRenderer : PageRenderer
{
public override void DidMoveToParentViewController(UIViewController parent)
{
base.WillMoveToParentViewController(parent);
var titleView = new UITextView();
var page = this.Element as ContentPage;
try
{
if (!string.IsNullOrEmpty(page.Title))
{
titleView.Text = page.Title;
titleView.TextColor = UIColor.White;
titleView.Font = UIFont.SystemFontOfSize(17, UIFontWeight.Regular);
var bgColor = page.BackgroundColor;
titleView.BackgroundColor = UIColor.FromRGBA((int)bgColor.R, (int)bgColor.G, (int)bgColor.B, 0);
parent.NavigationItem.TitleView = titleView;
parent.NavigationItem.TitleView.ContentMode = UIViewContentMode.ScaleAspectFit;
}
}
catch (Exception e)
{
}
}
}
}

我还删除了之前放在AppDelegate文件和App.xaml.cs文件中的所有代码。我保留了ViewModels中的代码,因为它将后退按钮更改为白色,并且我删除了之前创建的新NagivationPage类。

我将解释为什么我做了你在那里看到的一些事情:

为了更改标题,我创建了一个UITextView()并将其设置为父页面NavigationItem.TitleView。我设置titleView.Text = page.Title;因为我的原始页面已经有标题,所以我只是重复使用它。backgroundcolor我必须做所有这些事情,所以backgroundcolor属性以我想要的方式工作。

这个DidMoveToParentViewController功能只是为了在从PrismNavigationAsync之前完成所有这些工作。

最新更新