Xamarin.ios的CustomTabRenderer
问题描述:
我几个月来一直在使用Xamarin,并且偶尔会遇到一些麻烦。我正在使用自定义渲染器来更改我的选项卡的外观。在不同标签之间切换时,它工作得很好。然而,当我浏览到我的tabbedPage使用:Xamarin.ios的CustomTabRenderer
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);
}
}
}
}
答
问题似乎当我升级我从ios9检测到ios10设备已经悄悄(无法确认这一点,因为我无法找到可以测试的ios9设备)。
通过对这篇文章的回答:How do I override the Xamarin Forms TabbedPage item fonts for iOS?,让我尝试了各种各样的东西在我自己的ViewWillAppear。
该问题已通过更改我的代码以在我的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);
}
答
我相信,你只需要更换:
CurrentPage = yourPage;
与
SelectedItem = yourPage;
我试过这个,也是在我的自定义渲染器中,但它给了我相同的结果。当我删除item.Image = item.Image.ImageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal); mapPage图标是白色的,而另一个是灰色的(表示它被设置为SelectedIcon),但文本大小与未选中的相同 – Valkyrie