UITabBarController使用

来源:互联网 发布:cnc编程用什么软件 编辑:程序博客网 时间:2024/06/15 10:03

UITabBarController是IOS总常用的ViewController之一,下面简单介绍一下其使用:

1.UITabBarController主要用于管理多个控制器


如上图tabBar的每一项对应一个viewController

2.根据提供的文档,UITabBarController通常作为整个程序的rootViewController,一般不建议添加到其他的 viewController中(之前将其添加到UINavigationController没什么问题,不过退出应用有时候调用生命周期函数有问题),使用一般如下:

self.window.rootViewController=tabController;//<pre name="code" class="html">tabController为自定义的UITabBarController

3.设置UITabBar的背景颜色:

[[UITabBar appearance] setBarTintColor:color];self.tabBar.translucent=NO;//是否透明
4.设置UITabBarItem的字体颜色:

[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:color,NSForegroundColorAttributeName, nil] forState:UIControlStateSelected];//可以根据不同状态设置<pre name="code" class="html">UIControlStateSelected表明是选中后的状态的字颜色

5.设置UITabBarItem的图片和选中后的图片,和图片下面显示的文字:

[childVC.tabBarItem setImage:image];//正常情况显示图片[childVC.tabBarItem setSelectedImage:selectedImage];//选中后显示的图片[childVC.tabBarItem setTitle:title];//图片下面的文字
6.如果需要调整图片和文字之间的距离可以使用:

//设置字体的间距向上5pt UIOffset titleOffset=UIOffsetMake(0, -5); [childVC.tabBarItem setTitlePositionAdjustment:titleOffset];
7.在使用UITabBarController时候,一般是和UINavigationController结合使用,把UITabBarItem对应的controller放到一个UINavigationController中:

-(void)addChildVC:(UIViewController*)childVC title:(NSString*)title img:(NSString*)img selectedImg:(NSString*)selectedImg{    //childVC.title=title;    UIImage *image=[UIImage imageNamed:img];    UIImage *selectedImage=[UIImage imageNamed:selectedImg];    image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];    selectedImage = [selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];        [childVC.tabBarItem setImage:image];    [childVC.tabBarItem setSelectedImage:selectedImage];    [childVC.tabBarItem setTitle:title];    //设置字体的间距向上5pt    UIOffset titleOffset=UIOffsetMake(0, -5);    [childVC.tabBarItem setTitlePositionAdjustment:titleOffset];    UINavigationController *navController=[[UINavigationController alloc]initWithRootViewController:childVC];//将<span style="font-family: Arial;">UITabBarItem对应的controller放到一个</span><span style="font-family: Arial;">UINavigationController中</span>    [self addChildViewController:navController];    //[_vcArray addObject:navController];}

8.需要声明的是每个在UITabBarController中的viewController,UITabBarController都会为其增加一个tabBarItem属性,需要设置时,直接使用即可。

可以参考:
@interface UIViewController (UITabBarControllerItem)@property(null_resettable, nonatomic, strong) UITabBarItem *tabBarItem; // Automatically created lazily with the view controller's title if it's not set explicitly.@property(nullable, nonatomic, readonly, strong) UITabBarController *tabBarController; // If the view controller has a tab bar controller as its ancestor, return it. Returns nil otherwise.@end

9.如果想用代码设置选中UITabBarItem可以使用:

self.selectedIndex=index;








0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 魅蓝手机的图库没了怎么办 魅蓝5s屏幕碎了怎么办 魅族手机中间的home键失灵怎么办 魅族手机突然关机开不了机了怎么办 魅蓝3s卡顿怎么办视频 魅蓝3s手机太卡怎么办 红米4x外放破音怎么办 魅族手机刷机后内存变小了怎么办 手机用久了内存越来越小怎么办魅族 贴了车膜左右后视镜反光怎么办 贴手机膜时酒精没了怎么办 魅蓝e外屏幕碎了怎么办 贴的手机膜边缘翘起来着怎么办 手机后面贴的膜都是胶怎么办 车漆外面一层保护膜被划开了怎么办 美团骑手gps信号弱时怎么办 华为荣耀4x充电插口坏了怎么办 华为畅享5s开不了机怎么办 华为梅特10忘记账号密码怎么办 华为荣耀8充电接口坏了怎么办 华为手机锁屏了自动开屏怎么办 支付宝收货地址写错了怎么办 支付宝领海报地址写错了怎么办 手机总是弹出日历已停止运行怎么办 华为mate8忘了锁屏密码怎么办 红米4a打王者卡怎么办 苹果5s软件更新密码忘记了怎么办 华为荣耀P9进水了没有声音怎么办 华为荣耀7原相机不能拍照怎么办 华为手机触屏密码忘记了怎么办 华为荣耀10屏锁密码忘了怎么办 华为手机开机卡在开机界面怎么办 苹果6s国行不能用移动4g怎么办 苹果手机32g内存不够用怎么办 华为荣耀6x忘记了密码怎么办 百度粉色衣服被洗变色了怎么办 粉色衣服放进洗衣机洗变色了怎么办 红米note5a应用锁忘了怎么办 索尼手机死机了怎么办不可拆卸电池 华为手机一直停留在开机画面怎么办 5s用11.4太卡了怎么办