xamarin IOS对xamarin.forms的Tabbedpage的renderer实现

IOS下tabbar默认样式个人感觉已经很完美了,但有时候架不住奇葩的设计需求,需要进行一些更改.

1、标题字体大小

//字体设置
                var textAttributes = new UITextAttributes();
                textAttributes.Font = UIFont.FromName("ChalkboardSE-Bold", 18.0F);
                textAttributes.TextColor = UIColor.Black;
                UITabBarItem.Appearance.SetTitleTextAttributes(textAttributes, UIControlState.Normal);

2、图标隐藏

//标题位置偏移,填补icon空白
                UITabBarItem.Appearance.TitlePositionAdjustment = new UIOffset(0, -10);


3、选中项背景色设置

//tab个数,用以计算每个item的长度
                var itemCount = ((TabbedPage)e.NewElement).Children.Count;
                //通过背景图片设置,翻遍了api,貌似没发现有直接设置选中项背景色的接口
                var selectedImage = ImageFromColor(UIColor.Clear.FromHexString("#4b6f9f", 1.0f), new CGSize(TabBar.Bounds.Width / itemCount, TabBar.Bounds.Height));
                TabBar.SelectionIndicatorImage = selectedImage;
/// <summary>
        /// 画背景色
        /// </summary>
        /// <param name="color"></param>
        /// <param name="size"></param>
        /// <returns></returns>
        private UIImage ImageFromColor(UIColor color, CGSize size)
        {
            UIGraphics.BeginImageContext(size);
            var context = UIGraphics.GetCurrentContext();
            context.SetFillColor(color.CGColor);
            context.FillRect(new CGRect(new CGPoint(0, 0), size));
            UIImage img = UIGraphics.GetImageFromCurrentImageContext();
            UIGraphics.EndImageContext();
            return img;
        }

4、选中项字体设置

/// <summary>
        /// 选中项字体设置
        /// </summary>
        public override UIViewController SelectedViewController
        {
            get
            {
                var selectedTextAttributes = new UITextAttributes();
                selectedTextAttributes.Font = UIFont.FromName("ChalkboardSE-Bold", 18.0F);
                selectedTextAttributes.TextColor = UIColor.White;
                if (base.SelectedViewController != null)
                {
                    base.SelectedViewController.TabBarItem.SetTitleTextAttributes(selectedTextAttributes, UIControlState.Normal);                    
                }
                return base.SelectedViewController;
            }
            set
            {
                base.SelectedViewController = value;
                foreach (var viewController in base.ViewControllers)
                {
                    var normalTextAttributes = new UITextAttributes();
                    normalTextAttributes.Font = UIFont.FromName("ChalkboardSE-Bold", 18.0F);
                    normalTextAttributes.TextColor = UIColor.Black;
                    viewController.TabBarItem.SetTitleTextAttributes(normalTextAttributes, UIControlState.Normal);
                }
            }
        }



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值