我正在使用Xamarin.Forms并尝试更改iOS上导航栏的背景颜色.
我有一个继承自NavigationPage的自定义导航栏类,带有可绑定属性和构造函数,用于设置导航栏的颜色.根据我的理解,导航栏上面有一个默认的背景(黑色)Xamarin.Forms导航背景.我可以使用SetColor()方法设置背景颜色(见下文).然而,它留下了一条黑线,这是导航栏(iOS)的背景,如图所示.图片链接
现在,我正在尝试将iOS导航栏背景颜色设置为白色或透明.我花了很多时间但没有任何效果.有人可以协助如何将背景设置为白色.
//PCL class
public class CustomNavigationalPage : NavigationPage
{
public static readonly BindableProperty BarBgColorProperty =
BindableProperty.
Create<CustomNavigationalPage, UIColor>
(p => p.BarBackgroundColorR, null);
public UIColor BarBackgroundColorR
{
get { return (UIColor)base.GetValue (BarBgColorProperty); }
set { base.SetValue (BarBgColorProperty, value); }
}
public NavigationalPageCustomized() : base()
{
SetColor();
}
void SetColor()
{
BarBackgroundColor = Color.Transparent;
BarTextColor = Color.Blue;
}
}
Run Code Online (Sandbox Code Playgroud)
导航栏渲染器类:
[assembly: ExportRenderer (typeof (CustomNavigationalPage), typeof (CustomNavigationPageRenderer))]
namespace project.iOS
{
public class CustomNavigationPageRenderer : NavigationRenderer
{
public CustomNavigationPageRenderer()
{
// UINavigationBar.Appearance.SetBackgroundImage (UIImage.FromFile ("navbg.png"), UIBarMetrics.Default);
}
protected override void OnElementChanged (VisualElementChangedEventArgs args)
{
base.OnElementChanged (args);
var nb = (NavigationalPageCustomized) Element;
if (nb != null)
{
nb.BarBackgroundColorR = UIColor.White;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
在Xamarin.forms的PCL中试用此代码.在App.xaml.cs的构造函数中更改以下代码
public App()
{
MainPage = new NavigationPage(new Page1())
{
BarBackgroundColor = Color.Gray
};
}
Run Code Online (Sandbox Code Playgroud)
您可以在全局 App.xaml 文件中进行设置
<Style TargetType="NavigationPage">
<Setter Property="BarBackgroundColor" Value="Blue"/>
<Setter Property="BarTextColor" Value="White"/>
</Style>
Run Code Online (Sandbox Code Playgroud)
更改为您自己的颜色
小智 2
这过去需要自定义渲染器,但在 XF 1.3 中不再需要。NavigationPage 现在具有 BarBackgroundColor 和 BarTextColor 属性,看起来效果很好。不幸的是,尽管没有自定义渲染器(我发现),但无法更改字体。
| 归档时间: |
|
| 查看次数: |
18873 次 |
| 最近记录: |