首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CustomTabRenderer for Xamarin.ios

CustomTabRenderer for Xamarin.ios
EN

Stack Overflow用户
提问于 2017-06-19 13:02:21
回答 2查看 900关注 0票数 1

我已经用了几个月了,现在还会遇到一些麻烦。我正在使用一个自定义渲染器来改变我的标签的外观和感觉。当切换到不同的选项卡时,它工作得非常好。但是,当我使用以下方法导航到我的tabbedPage时:

代码语言:javascript
复制
Children.Add(new UnitMapPage { Title = "Map", Icon = "map.png" });
CurrentPage = mapPage;

页面在正确的选项卡上打开,但是TabbarItem的文本看起来与未选择的项完全相同。一旦你点击任何选项卡,它就会改变到正确的样式。这是我的定制渲染器。

代码语言:javascript
复制
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);
            }
        }
    }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 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解决了这个问题不知道这是否是最好的方法,但它做到了!

代码语言:javascript
复制
 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);
    }
票数 0
EN

Stack Overflow用户

发布于 2017-06-19 14:13:50

我相信你只需要交换一下:

代码语言:javascript
复制
CurrentPage = yourPage;

使用

代码语言:javascript
复制
SelectedItem = yourPage;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44631340

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档