我已经用了几个月了,现在还会遇到一些麻烦。我正在使用一个自定义渲染器来改变我的标签的外观和感觉。当切换到不同的选项卡时,它工作得非常好。但是,当我使用以下方法导航到我的tabbedPage时:
Children.Add(new UnitMapPage { Title = "Map", Icon = "map.png" });
CurrentPage = mapPage;页面在正确的选项卡上打开,但是TabbarItem的文本看起来与未选择的项完全相同。一旦你点击任何选项卡,它就会改变到正确的样式。这是我的定制渲染器。
class CustomTabRenderer : TabbedRenderer
{
private UnitViewModel _unitViewModel;
private TabbedPage _unitPage;
protected override void OnElementChanged(VisualElementChangedEventArgs e)
{
base.OnElementChanged(e);
if (e.NewElement != null) {
var unitPage = (TabbedPage)e.NewElement;
_unitPage = unitPage;
_unitViewModel = (UnitViewModel)_unitPage.BindingContext;
_unitViewModel.PropertyChanged += OnElementPropertyChanged;
}
// Set Text Font for unselected tab states
UITextAttributes normalTextAttributes = new UITextAttributes();
normalTextAttributes.Font = UIFont.SystemFontOfSize(10.0F, UIFontWeight.Regular); // unselected
normalTextAttributes.TextColor = UIColor.White;
UITabBarItem.Appearance.SetTitleTextAttributes(normalTextAttributes, UIControlState.Normal);
}
void OnElementPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
if (e.PropertyName == "AlertCount")
{
if (TabBar.Items != null)
{
foreach (var item in TabBar.Items)
{
item.Image = item.Image.ImageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal);
if (item.Title == "History" && _unitViewModel != null)
{
if (_unitViewModel.AlertCount > 0)
item.BadgeValue = _unitViewModel.AlertCount.ToString();
else
item.BadgeValue = null;
}
}
}
}
}
public override void ViewWillAppear(bool animated)
{
if (TabBar.Items != null)
{
foreach (var item in TabBar.Items)
{
item.Image = item.Image.ImageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal);
if (item.Title == "History" && _unitViewModel != null)
{
if (_unitViewModel.AlertCount > 0)
item.BadgeValue = _unitViewModel.AlertCount.ToString();
else
item.BadgeValue = null;
}
}
}
base.ViewWillAppear(animated);
}
public override UIViewController SelectedViewController
{
get
{
UITextAttributes selectedTextAttributes = new UITextAttributes();
selectedTextAttributes.Font = UIFont.SystemFontOfSize(12.0F, UIFontWeight.Heavy); // SELECTED
if (base.SelectedViewController != null)
{
base.SelectedViewController.TabBarItem.SetTitleTextAttributes(selectedTextAttributes, UIControlState.Normal);
}
return base.SelectedViewController;
}
set
{
base.SelectedViewController = value;
foreach (UIViewController viewController in base.ViewControllers)
{
UITextAttributes normalTextAttributes = new UITextAttributes();
normalTextAttributes.Font = UIFont.SystemFontOfSize(10.0F, UIFontWeight.Regular); // unselected
normalTextAttributes.TextColor = UIColor.White;
viewController.TabBarItem.SetTitleTextAttributes(normalTextAttributes, UIControlState.Normal);
}
}
}
}发布于 2017-06-20 09:27:18
当我将正在测试的设备从ios9升级到ios10时,这个问题似乎悄然出现(不能确认这一点,因为我找不到可以测试这个的ios9设备)。
阅读这篇文章的答案:How do I override the Xamarin Forms TabbedPage item fonts for iOS?,让我在我自己的How do I override the Xamarin Forms TabbedPage item fonts for iOS?中尝试各种东西。
通过更改我的代码以在我的ViewWillAppear.中设置selectedTextAttributes解决了这个问题不知道这是否是最好的方法,但它做到了!
public override void ViewWillAppear(bool animated)
{
if (TabBar.Items != null)
{
foreach (var item in TabBar.Items)
{
item.Image = item.Image.ImageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal);
if (item.Title == "History" && _unitViewModel != null)
{
if (_unitViewModel.AlertCount > 0)
item.BadgeValue = _unitViewModel.AlertCount.ToString();
else
item.BadgeValue = null;
}
if (item.Title.Equals(_unitPage.CurrentPage.Title))
{
UITextAttributes selectedTextAttributes = new UITextAttributes();
selectedTextAttributes.Font = UIFont.SystemFontOfSize(12.0F, UIFontWeight.Heavy); // SELECTED
if (base.SelectedViewController != null)
{
base.SelectedViewController.TabBarItem.SetTitleTextAttributes(selectedTextAttributes, UIControlState.Normal);
}
}
}
}
base.ViewWillAppear(animated);
}发布于 2017-06-19 14:13:50
我相信你只需要交换一下:
CurrentPage = yourPage;使用
SelectedItem = yourPage;https://stackoverflow.com/questions/44631340
复制相似问题