UI学习

本文介绍了UITabBarController的基本使用方法及其定制技巧,包括如何设置自定义的TabBarItem、使用代理方法和NSUserDefaults进行状态保存,以及如何实现完全自定义的tabBar。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

day7 UITabBarController


UITabBarController
标签控制器
系统tabBarItem

1. UITabBarController
UITabBarItem
代理方法
NSUserDefaults
System item:
UITabBarItem alloc initWithTabBarSystemItem:tag:
We can’t set the title and image if use the systemItem
用系统预置好item无法设置标题和图片

Noted the loadView viewDidLoad and willWillAppear executed only for current controller
.moreNavigationController not in viewControllers
selectedIndex
selectedViewController
delegate方法的使用
NSUserDefault的使用
// 注:住偏好设置里面存的值比较有限,只支持以下几种:
// Array Dictionary BOOL Data Date Number String简单的类型
// [userDefault setBool:(BOOL) forKey:(NSString *)];
// [userDefault boolForKey:(NSString *)]
// [userDefault setInteger:(NSInteger) forKey:(NSString *)];
// [userDefault integerForKey:(NSString *)]
// 往偏好设置中存储Data不推荐用
// 偏好设置使用场景是一些简单的用户操作,设置等,不宜存储大容量和复杂的数据
// 注:往偏好里面基于同一个key赋值会把原来相同key对应的值覆盖掉
——————————————————————————————
2. 定制TabBarController & item  
项目爱限免界面搭建




NSArray *commonImgNames = @[@"tabbar_limitfree", @"tabbar_reduceprice", @"tabbar_appfree", @"tabbar_subject", @"tabbar_rank"];
NSArray *titles = @[@"限免", @"降价", @"免费", @"专题", @"热榜"];

设置
vc.tabBarItem.image
// vc.title = @“1512棒棒哒”;
vc.tabBarItem.title = …
也可以这样:
[[UITabBarItem alloc] initWithTitle:image:tag:]

注意:the vc.title 相当于下面这两句
vc.navigationItem.title = …
vc.tabBarItem.title = …

// 设置微章
vc.tabBarItem.badgeValue = @"4";

[item setFinishedSelectedImage:withFinishedUnselectedImage:]; is deprecated.

TabBar:
tintColor
barTintColor
backgroundImage

通过UIAppearance设置item的标题颜色
[[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor orangeColor]} forState:UIControlStateSelected];

定制item,设置普通图片、选中图片和标题
第一种方式:
vc.tabBarItem.image = …
vc.tabBarItem.selectedImage = … noted the renderingMode of image
vc.tabBarItem.title

上面三句代码对应:
[[UITabBarItem alloc] initWithTitle:image:selectedImage:]

设置选中标题颜色:
[[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName :[UIColor orangeColor]} forState:UIControlStateSelected];

限免
tabbar_limitfree.png
tabbar_limitfree_press.png

降价
tabbar_reduceprice.png
tabbar_reduceprice_press.png

免费
tabbar_appfree
tabbar_appfree_press

专题
tabbar_subject
tabbar_subject_press

热榜
tabbar_rank
tabbar_rank_press
——————————————————————————————
3. 自定制tabBar

思路:
1. 把系统的tabBar隐藏掉
2. 新建imageView,在上面加一个个button 并给button设置图片
3. 当点击其中一个button时触发相应的方法并跳转到对应的控制器.selectedViewController = …



—————————————————————————————
定制TabBarController 2

code4.app

tab_feed.png tab_live      tab_feed_profile.png tab_messages.png
给ViewController做扩展
+ (instancetype) viewControllerWithTabTitle:(NSString*)title image:(UIImage*)image selectedImage:(UIImage *)selectedImage

实现中间大按钮的思路:加一个按钮,把这个按钮放在中间位置
CGFloat heightDifference = image.size.height - self.tabBar.frame.size.height;
if (heightDifference < 0)
    button.center = self.tabBar.center;
else
{
    CGPoint center = self.tabBar.center;
    center.y = center.y - heightDifference/2.0;
    button.center = center;
}
—————————————————————————————

作业:






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值