UITabBar

作者: 无邪8 | 来源:发表于2018-01-22 17:40 被阅读345次

    一、简介

    <<UITabBar类实现了一个选择两个或多个按钮的控制,并称之为项目。一个标签栏最常见的用途是实现一个模态接口攻一个项目的变化选择。如果你想暂时突出或不会改变某个项目的外观时,轻按按钮,使用一个UIToolbar对象。UITabBar类提供为用户定制能力重新排列标签栏,删除和酒吧中添加项目。您可以使用标签栏的委托,以增加这种行为。

    <<UITabBar对象控制在不同的任务、试图或模式间的切换,标签栏包含于标签栏控制器中,这是一个可以管理一系列自定义视图显示的程序对象。 一般和UITabBarConmutroller对象结合使用(也能独立使用tab bars作为独立控制)

    <<一个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 <UITabBarDelegate>delegate; //设置UITabBarDelegate代理

    三、UITabBar的标签属性

    1-->设置标签

    tabBar.items = itemsArray。

    @property(nullable, nonatomic, copy) NSArray<UITabBarItem *> *items;//get/set UITabBarItems,该数组必须包含的UITabBarItem对象 默认是nil 改变时没有动画效果 按顺序展示

    2--> 设置选中的标签

       tabBar.selectedItem = tabBarItemArray; // 设置tabBarItem为选中的标签

    @property(nullable, nonatomic, weak) UITabBarItem *selectedItem; //显示基于模式的反馈。默认为空

    3-> 设置标签与动画显示

        [tabBarController.tabBar setItems:tabBarItemArray animated:YES];

    - (void)setItems:(nullable NSArray *)items animated:(BOOL)animated; 

    四、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各种定制

    相关文章

      网友评论

        本文标题:UITabBar

        本文链接:https://www.haomeiwen.com/subject/guduoxtx.html