当前位置: 代码迷 >> 综合 >> xamarin IOS对xamarin.forms的Tabbedpage的renderer实现
  详细解决方案

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

热度:76   发布时间:2023-12-22 06:41:48.0

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);}}}



  相关解决方案