Xamarin.ios的CustomTabRenderer
我已经使用Xamarin几个月了,但仍不时遇到一些麻烦。我正在使用自定义渲染器更改选项卡的外观。在不同选项卡之间切换时,它工作得非常好。但是,当我使用以下命令导航到选项卡页时:Xamarin.ios的CustomTabRenderer,xamarin,xamarin.ios,xamarin.forms,custom-renderer,tabbedpage,Xamarin,Xamarin.ios,Xamarin.forms,Custom Renderer,Tabbedpage,我已经使用Xamarin几个月了,但仍不时遇到一些麻烦。我正在使用自定义渲染器更改选项卡的外观。在不同选项卡之间切换时,它工作得非常好。但是,当我使用以下命令导航到选项卡页时: Children.Add(new UnitMapPage { Title = "Map", Icon = "map.png" }); CurrentPage = mapPage; 页面将在正确的选项卡上打开,但TabbarItem的文本与未选中的项目完全相同。只要您点击任何选项卡,它就会更改为正确的样式。这是我的自定义
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);
}
}
}
}
我相信你只需要交换:
CurrentPage = yourPage;
与
我相信你只需要交换:
CurrentPage = yourPage;
与
当我将正在测试的设备从ios9升级到ios10时,问题似乎已经出现了(无法确认这一点,因为我找不到可以在其上进行测试的ios9设备) 阅读这篇文章的答案:,让我在自己的视图中尝试了各种事情 通过更改我的代码以设置我的视图中的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);
}
当我将正在测试的设备从ios9升级到ios10时,问题似乎已经出现了(无法确认这一点,因为我找不到可以在其上进行测试的ios9设备) 阅读这篇文章的答案:,让我在自己的视图中尝试了各种事情 通过更改我的代码以设置我的视图中的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);
}
我也在自定义渲染器中尝试了这一点,但它给出了相同的结果。当我删除item.Image=item.Image.ImageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)时;mapPage图标是白色的,而另一个是灰色的(这意味着它被设置为SelectedIcon),但文本大小与未选择的保持不变。我也在自定义渲染器中尝试了这一点,但它给出了相同的结果。当我删除item.Image=item.Image.ImageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)时;地图页面图标为白色,而另一个为灰色(表示它被设置为SelectedIcon),但文本大小与未选择的相同