博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
修改UITabBar样式 TintColor 和 Selected Tab Images in Xamarin.Forms iOS
阅读量:4313 次
发布时间:2019-06-06

本文共 2097 字,大约阅读时间需要 6 分钟。

修改UITabBar样式TintColor 和 Selected Tab Images in Xamarin.Forms iOS

如何在Xamarin.Forms中修改UITabBar样式?

首先新建MainTabbediOSPage继承于TabbedPage。

默认样式如下:

1. TintColor

想要得到如下效果:

只需在iOS项目中,AppDelegate 中的 FinishedLaunching 方法内添加如下代码:

1 UITabBar.Appearance.SelectedImageTintColor = UIColor.FromRGB(118,53,235);

 

2. Selected Tab Images

如果想要在Tab切换时,修改 Tab Image需要用到自定义渲染。

期望效果:

 

首先在iOS项目中Resource文件夹下添加Tab Images的资源文件:

(说明:此处如icon_chat是未选中图片资源,icon_chat_sel是选中图片资源)

Custom Renderer代码如下:

1 using System; 2 using System.Collections.Generic; 3 using System.Diagnostics; 4 using System.Linq; 5 using System.Text; 6  7 using Foundation; 8 using UIKit; 9 using Xamarin.Forms;10 using Xamarin.Forms.Platform.iOS;11 using XFPractice.iOS.Renderer;12 using XFPractice.Pages;13 14 [assembly: ExportRenderer(typeof(MainTabbediOSPage), typeof(MyTabPageRenderer))]15 namespace XFPractice.iOS.Renderer16 {17     18     public class MyTabPageRenderer: TabbedRenderer19     {20         public override void ViewWillAppear(bool animated)21         {22             if (TabBar?.Items == null)23                 return;24            25             if (Element is TabbedPage tabs)26             {27                 for (int i = 0; i < TabBar.Items.Length; i++)28                 {29                     UpdateItem(TabBar.Items[i], tabs.Children[i].Icon);30                 }31             }32             base.ViewWillAppear(animated);33         }34 35         void UpdateItem(UITabBarItem item, string icon)36         {37             if (item == null)38                 return;39             try40             {41                 icon = icon + "_sel";42                 if (item?.SelectedImage?.AccessibilityIdentifier == icon)43                     return;44                 item.SelectedImage = UIImage.FromBundle(icon);45                 item.SelectedImage.AccessibilityIdentifier = icon;46             }47             catch (Exception ex)48             {49                 Console.WriteLine("Unable to set selected icon: " + ex);50             }51 52         }53     }54 }
View Code

 

转载于:https://www.cnblogs.com/devin_zhou/p/8259376.html

你可能感兴趣的文章
Windows XP硬盘安装Ubuntu 12.04双系统图文详解
查看>>
【资料】哈代&拉马努金相关,悼文,哈佛演讲,及各种杂七杂八资料整理
查看>>
Use weechat (IRC client) on OS X. MacBook Pro
查看>>
Luogu P3616 富金森林公园
查看>>
[Nowcoder] 六一儿童节(拼多多)
查看>>
centos6.7用yum安装redis解决办法及IP限制配置
查看>>
用DataReader 分页与几种传统的分页方法的比较
查看>>
看起来像是PS的照片,实际上却令人难以置信!
查看>>
随笔一则
查看>>
WEB 小案例 -- 网上书城(一)
查看>>
加入博客园八个月了
查看>>
怎样实现前端裁剪上传图片功能
查看>>
python flask 如何修改默认端口号
查看>>
Map<String,Object> map=new HashMap<String,Object>详解
查看>>
实现tap的多种方式
查看>>
UVA - 10494 If We Were a Child Again
查看>>
html5 canvas 渲染像素混合模式
查看>>
【hexo】01安装
查看>>
CI框架源码学习笔记2——Common.php
查看>>
005---书籍添加和编辑的提交数据
查看>>