一,简介
<
<< 一个 UITabBarController 只有一个 TabBar.
<<继承关系:UITabBar --> UIView -->UIResponder-->NSObject
格式为
1--> 设置 item 位置的样式(属性的作用)
typedef NS_ENUM(NSInteger, UITabBarItemPositioning) {
UITabBarItemPositioningAutomatic,
UITabBarItemPositioningFill,
UITabBarItemPositioningCentered,
} NS_ENUM_AVAILABLE_IOS(7_0);(如果属性有枚举类型的话,这里会有枚举类型说明
tabBar.itemPositioning = UITabBarItemPositioningCentered;(这是具体的例子)
@property(nonatomic) UITabBarItemPositioning itemPositioning NS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR __TVOS_PROHIBITED;(这是属性的说明)
二,UITabBar 的属性 (属性的顺序与苹果 API 一致)
1--> 声明 UITabBarDelegate 代理
tabBar .delegate = self;// 声明代理
@property(nullable, nonatomic, weak) id delegate; // 设置 UITabBarDelegate 代理
三,UITabBar 的标签属性
1--> 设置标签
tabBar.items = itemsArray.
@property(nullable, nonatomic, copy) NSArray *items;//get/set UITabBarItems,该数组必须包含的 UITabBarItem 对象 默认是 nil 改变时没有动画效果 按顺序展示
2--> 设置选中的标签
tabBar.selectedItem = tabBarItemArray; // 设置 tabBarItem 为选中的标签
@property(nullable, nonatomic, weak) UITabBarItem *selectedItem; // 显示基于模式的反馈.默认为空
3-> 设置标签与动画显示
[tabBarController.tabBar setItems:tabBarItemArray animated:YES];
@property (nonatomic, getter=isHighlighted) BOOL highlighted NS_AVAILABLE_IOS(3_0); // 设置是否为高亮状态,默认为普通状态
四,UITabBar 的自定义标签顺序属性(注意使用 UI TabBarController 饰不能使用此方法,自定义时使用)
1--> 设置自定义标签
[tabBar beginCustomizingItems:@[item1, item3, item2]];// 让用户自定义 items 的布局, 系统 会自动弹出一个带有 Done 按钮的视图
- (void)beginCustomizingItems:(NSArray *)items __TVOS_PROHIBITED; // 重新排序.这将显示一个包含所有条目的表单,允许用户更改 / 重新排序项目,并在顶部显示一个 "Done" 按钮.列出所有可以重新排序的项目.总是把一张纸动起来.未列出的可见项是固定的.
2--> 完成标签布局
这个方法博主是在找不到具体怎么调用的,只能贴出苹果官方 API- endCustomizingAnimated .希望有人知道的能告诉一下,来张老师的图以安慰自己.
- (BOOL)endCustomizingAnimated:(BOOL)animated __TVOS_PROHIBITED;
3--> 是否正在自定义标签布局
BOOL customizing=imageView.customizing; // 获取旋转状态
#if UIKIT_DEFINE_AS_PROPERTIES//UIKIT 定义的属性
@property(nonatomic, readonly, getter=isCustomizing) BOOL customizing __TVOS_PROHIBITED;
#else
- (BOOL)isCustomizing __TVOS_PROHIBITED;
#endif
五,UITabBar 的颜色属性
1--> 设置渲染颜色,会影响选中字体和图案的渲染
tabBar.tintColor=[UIColor redColor];
@property(null_resettable, nonatomic, strong) UIColor *tintColor NS_AVAILABLE_IOS(5_0);//tintColor 是通过 superview 层次结构继承的
2--> 设置导航栏的颜色
tabBar.barTintColor = [UIColor whiteColor];
@property(nullable, nonatomic, strong) UIColor *barTintColor NS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR;// barTintColor 会影响背景颜色, iOS7 出现的新属性, 用来代替 tintColor 的作用
3--> 未选中的 Item 的颜色 IOS10 可用
tabBar.unselectedItemTintColor = [UIColor redColor];
@property (nonatomic, readwrite, copy, nullable) UIColor *unselectedItemTintColor NS_AVAILABLE_IOS(10_0) UI_APPEARANCE_SELECTOR;// 在这个标签栏中未选中的项将被着色为这个颜色.将这个值设置为 nil 表明 UITabBar 应该使用它的默认值.
4--> 选中的 Item 的 Image 颜色
tabBar.selectedImageTintColor = [UIColor redColor];
@property(nullable, nonatomic, strong) UIColor *selectedImageTintColor NS_DEPRECATED_IOS(5_0,8_0,"Use tintColor") UI_APPEARANCE_SELECTOR __TVOS_PROHIBITED;// 弃用属性,被 tintColor 代替
六,UITabBar 的背景图案属性
1--> 设置导航栏背景图案
[[UITabBar appearance] setBackgroundImage:[UIImage imageNamed:@"tabBarBackgroundImage"]];
@property(nullable, nonatomic, strong) UIImage *backgroundImage NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;// 即使不是通过 UIImage resizableImage 方法创建的,背景图像也会被修改.
2--> 设置选中一个标签时,标签背后的选中提示图案 这个会出现在设置的 item 图案的后面
tabBar.selectionIndicatorImage=[UIImage imageNamed:@"tabBarBackgroundImage"];
@property(nullable, nonatomic, strong) UIImage *selectionIndicatorImage NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
3--> 设置阴影的背景图案
[[UITabBarappearance] setShadowImage:[UIImagenew]];
@property(nullable, nonatomic, strong) UIImage *shadowImage NS_AVAILABLE_IOS(6_0) UI_APPEARANCE_SELECTOR;// 默认是 nil.当非 nil 时,自定义阴影图像来显示而不是默认阴影图像.对于要显示的自定义阴影,还必须设置自定义背景图像 (如果使用默认背景图像,则使用默认的阴影图像).
七,UITabBar 的背景图案属性
1--> 设置标签 item 的位置模式
// 枚举如下
typedef NS_ENUM(NSInteger, UITabBarItemPositioning) {
UITabBarItemPositioningAutomatic,// 自动
UITabBarItemPositioningFill,// 充满
UITabBarItemPositioningCentered,// 中心
} NS_ENUM_AVAILABLE_IOS(7_0);
[UITabBar appearance].itemPositioning=UITabBarItemPositioningCentered;
@property(nonatomic) UITabBarItemPositioning itemPositioning NS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR __TVOS_PROHIBITED;// 默认是 UITabBarItemPositioningAutomatic.标签栏项是水平填充的
2--> 设置 item 宽度
[UITabBar appearance].itemWidth=50;
@property(nonatomic) CGFloat itemWidth NS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR;// 设置值为 0 或小于 0 的值将被解释为系统定义的宽度
3-->item 间隙
[UITabBar appearance].itemSpacing=5;
@property(nonatomic) CGFloat itemSpacing NS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR;// 设置值为 0 或小于 0 的值将被解释为系统定义的间隔
4--> 设置标签栏风格, 默认高度 49
typedef NS_ENUM(NSInteger, UIBarStyle) {
UIBarStyleDefault = 0,// 默认样式
UIBarStyleBlack = 1,// 黑色
UIBarStyleBlackOpaque = 1, // 弃用属性
UIBarStyleBlackTranslucent = 2, // 弃用属性
} __TVOS_PROHIBITED;
tabBar.barStyle =UIBarStyleBlack;
@property(nonatomic) UIBarStyle barStyle NS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR __TVOS_PROHIBITED;//UIBarStyleDefault 和 UIBarStyleBlack 来定义 UINavigationBar 样式,并且用 setTranslucent: 方法来设置透明与否
4--> 设置 item 是否透明
[UITabBar appearance].translucent =NO;
@property(nonatomic,getter=isTranslucent) BOOL translucent NS_AVAILABLE_IOS(7_0);// 设置 tabBar 的半透明属性 translucent 设置为 NO,默认为 YES, 若保留半透明效果,设置的颜色会与正常的颜色有色差
八,UITabBar 的 UITabBarDelegate
1,用户选中某个 UITabBarItem
- (void)tabBar:(UITabBar*)tabBar didSelectItem:(UITabBarItem*)item{
// 判断本次点击的 UITabBarItem 是否和上次的一样
if(item ==self.lastItem) {
// 一样就发出通知
[[NSNotificationCenterdefaultCenter] postNotificationName:@"LLTabBarDidClickNotification"object:niluserInfo:nil];
}
// 将这次点击的 UITabBarItem 赋值给属性
self.lastItem = item;
}
- (void)tabBar:(UITabBar*)tabBar didSelectItem:(UITabBarItem*)item;// 用户选择新视图时调用
标注:以下四个代理方法是当 Items>=6 个时,当进入 More 页面时,开始或结束 Item 编辑状态的相关回调
2,将要开始编辑标签时
- (void)tabBar:(UITabBar*)tabBar willBeginCustomizingItems:(NSArray *)items{
NSLog(@" 将要开始自定制 item");
}
- (void)tabBar:(UITabBar *)tabBar willBeginCustomizingItems:(NSArray *)items __TVOS_PROHIBITED; // 在自定义表显示之前调用.标签是标签项目列表
3,已经开始编辑标签时
- (void)tabBar:(UITabBar*)tabBar didBeginCustomizingItems:(NSArray *)items{
NSLog(@" 已经开始自定制 item");
}
- (void)tabBar:(UITabBar *)tabBar didBeginCustomizingItems:(NSArray *)items __TVOS_PROHIBITED; // 自定义表显示后调用.标签是标签项目列表
4,将要结束自定制 item
- (void)tabBar:(UITabBar*)tabBar willEndCustomizingItems:(NSArray *)items changed:(BOOL)changed {
NSLog(@" 将要结束自定制 item");
}
- (void)tabBar:(UITabBar *)tabBar willEndCustomizingItems:(NSArray *)items changed:(BOOL)changed __TVOS_PROHIBITED; // 在自定义表隐藏之前调用.标签是标签项目列表.
5,已经结束自定制 item
- (void)tabBar:(UITabBar*)tabBar didEndCustomizingItems:(NSArray *)items changed:(BOOL)changed{
NSLog(@" 将要结束自定制 item");
}
- (void)tabBar:(UITabBar *)tabBar didEndCustomizingItems:(NSArray *)items changed:(BOOL)changed __TVOS_PROHIBITED; // 自定义表被隐藏后调用.标签是标签项目列表.
参考
iOS 开发中 UITabBarController-- 标签控制器的使用 UINavigationBar - 使用总结 iOS 开发中 UITabBarController-- 标签控制器的使用 [ios]tabbar 超过 5 个后出现的 more 标签文字修改 以及 more,configure 界面 修改问题 iOS Tabbar 各种定制
来源: http://www.jianshu.com/p/1b3e868df606