美文网首页
关于UITabBar

关于UITabBar

作者: 沁晓Chr | 来源:发表于2015-12-22 18:21 被阅读2569次

    关于UITabBar
    btn只代表一个按钮,(按钮这里提到两种UIButon和UIBarItem,这里多指UIBarItem)
    UIBarItem和UIbutton有类似的功能的一个另类的btn

    UITabBar类是一个View,被UITabBarControler的tabBar(只读属性)强引用.(类似UItableViewController和默认的UItableView的关系).
    
    UITabBar内部btn(...item另类的btn,和btn类似但不同),由UITabBarControler.subviewController的(...item,另类btn)传入保存到一个强引用数组就是btn了(子控件的tabbarItem就是一个包装的btn控件,,另外这个Item是继承UIControl的所以,可以把这个Item强转为UIControl类型,然后用UIControl的addTarget监听按钮的点击)
    
    btn的点击事件产生,由UITabBar监听,并调用UITabBar.delegate的响应方法,切换UITabBarControler相应的子控制器(这个监听并非是继承子UIControl的addTarget,因为addTarget只能有一个目标监听者,覆盖了就没了,但是实际上可以使用把这个Item强转为UIControl类型,然后用UIControl的addTarget监听按钮的点击,所以猜测可能是通知或其它方式)
    
    
    UITabBar的代理默认是UITabBarControler(类似UItableViewController和默认的UItableView.delegate的关系)如图
    
    
    当UITabBarControler.tabBar.delegate被重新赋值的时候,会抛出一个异常如图
    但自定义的UITabBar的代理是可以随便设置的..
    当自定义的UITabBar成为了UITabBarControler.tabBar的时候,它的代理就不能被改变了
    猜测
    UITabBarControler.tabBar应该被重写了UITabBar.delegate的setter方法(类似KVO监听原理).
    
    另外UITabBarControler的代理UITabBarControler.delegate可以随意设置,并且代理方法中有可以监听UITabBarControler的子控制器切换的方法.
    
    自定义一个UIView代替UITabBar就要设置数组存item_btn,写代理协议监听按钮点击,UIView的代理设为控制器,控制器实现代理方法,切换自己的子控制器,切换方法如图(详细的看彩票的项目第一天)
    

    ![image](屏幕快照 2015-11-25 11.56.28.png)
    ![image](屏幕快照 2015-11-25 11.57.28.png)
    ![image](屏幕快照 2015-11-25 12.08.36.png)

    nav和tab中的bar和Item的区别

    两边的bar都是一个类UIView的容器
    
    tab的Item是一个相对bar独立的另类的btn(和toolBar中的Item是一样的UIBarItem)
    tab的Item是由自控制器的Item,另类的btn(和toolBar中的Item是一样的)传人保存的
    每一个子控制器对应一个Item所以独立, UITabBarItem : UIBarItem 
    当然子控制器的tabbarItem属性是TabbarControl中定义的UIViewControl的分类中添加的
    
    nav的Item是定义在navBar中的,并非是独立的,
    nav的Item是navBar内部已经订好的,需要时懒加载的,每个子控制器对应一个Bar中所有的Item而不是一个Item,和title(lable的字符串数据)等,所以子控制器的UINavigationItem : NSObject只是一个类似数组的数据库,包含所有按钮.
    当然子控制器的navbarItem属性是TabbarControl中定义的UIViewControl的分类中添加的
    navBar中并非只有Item按钮,navBar由navC控制,navItem由子控制器控制
    navBar和navItem又相对分开,和独立,各自有各自领域
    
    • self.navigationItem.rightBarButtonItem....(定义在navBar.h中),self.navigationItem子控制器的控制下的各种按钮继承自UIBarButtonItem,UIBarButtonItem继承自UIBarItem
    @property(nullable,nonatomic,copy) NSArray<UIBarButtonItem *> *leftBarButtonItems NS_AVAILABLE_IOS(5_0);
    @property(nullable,nonatomic,copy) NSArray<UIBarButtonItem *> *rightBarButtonItems NS_AVAILABLE_IOS(5_0);
    - (void)setLeftBarButtonItems:(nullable NSArray<UIBarButtonItem *> *)items animated:(BOOL)animated NS_AVAILABLE_IOS(5_0);
    - (void)setRightBarButtonItems:(nullable NSArray<UIBarButtonItem *> *)items animated:(BOOL)animated NS_AVAILABLE_IOS(5_0);
    
    /* By default, the leftItemsSupplementBackButton property is NO. In this case,
     the back button is not drawn and the left item or items replace it. If you
     would like the left items to appear in addition to the back button (as opposed to instead of it)
     set leftItemsSupplementBackButton to YES.
     */
    @property(nonatomic) BOOL leftItemsSupplementBackButton NS_AVAILABLE_IOS(5_0);
    
    // Some navigation items want to display a custom left or right item when they're on top of the stack.
    // A custom left item replaces the regular back button unless you set leftItemsSupplementBackButton to YES
    @property(nullable, nonatomic,strong) UIBarButtonItem *leftBarButtonItem;
    @property(nullable, nonatomic,strong) UIBarButtonItem *rightBarButtonItem;
    - (void)setLeftBarButtonItem:(nullable UIBarButtonItem *)item animated:(BOOL)animated;
    - (void)setRightBarButtonItem:(nullable UIBarButtonItem *)item animated:(BOOL)animated;
    
    tabBarItem:UIBarItem
    navBarItem.xxBarButtonItem:UIBarButtonItem:UIBarItem
    
    UIBarItem和UIbutton有类似的功能的一个另类的btn
    UIBarButtonItem中有这样一个方法在Item中包装一个View,可以包装一个UIbutton
    - (instancetype)initWithCustomView:(UIView *)customView;
    
    
    UITooBar中的Item是UIBarButtonItem,是UIBarButtonItem可以用自定义View,就是可以包装一个UIButton,,Xib中的UITooBar,可以拖UIButton等View,XIb自动包装成UIBarButtonItem
    
    

    ![image](屏幕快照 2015-11-25 15.31.38.png)
    ![image](屏幕快照 2015-11-30 19.59.51.png)

    UITabbarController的代理
    @protocol UITabBarControllerDelegate <NSObject>
    @optional
    - (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController NS_AVAILABLE_IOS(3_0);
    - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController;
    
    - (void)tabBarController:(UITabBarController *)tabBarController willBeginCustomizingViewControllers:(NSArray<__kindof UIViewController *> *)viewControllers NS_AVAILABLE_IOS(3_0);
    - (void)tabBarController:(UITabBarController *)tabBarController willEndCustomizingViewControllers:(NSArray<__kindof UIViewController *> *)viewControllers changed:(BOOL)changed NS_AVAILABLE_IOS(3_0);
    - (void)tabBarController:(UITabBarController *)tabBarController didEndCustomizingViewControllers:(NSArray<__kindof UIViewController *> *)viewControllers changed:(BOOL)changed;
    
    - (UIInterfaceOrientationMask)tabBarControllerSupportedInterfaceOrientations:(UITabBarController *)tabBarController NS_AVAILABLE_IOS(7_0);
    - (UIInterfaceOrientation)tabBarControllerPreferredInterfaceOrientationForPresentation:(UITabBarController *)tabBarController NS_AVAILABLE_IOS(7_0);
    
    - (nullable id <UIViewControllerInteractiveTransitioning>)tabBarController:(UITabBarController *)tabBarController
                          interactionControllerForAnimationController: (id <UIViewControllerAnimatedTransitioning>)animationController NS_AVAILABLE_IOS(7_0);
    
    - (nullable id <UIViewControllerAnimatedTransitioning>)tabBarController:(UITabBarController *)tabBarController
                animationControllerForTransitionFromViewController:(UIViewController *)fromVC
                                                  toViewController:(UIViewController *)toVC  NS_AVAILABLE_IOS(7_0);
    
    @end
    
    
    • UITabBarControllerDelegate 自己设置为application
    /** 记录上一次选中的子控制器的索引 */
    @property (nonatomic, assign) NSUInteger lastSelectedIndex;
    
    
    //@implementation AppDelegate
    #pragma mark - <UITabBarControllerDelegate>
    进入程序,默认选中tabBarController的第一个自控制器,但不会来这个方法,要点击之后才会来..
    所以记录上一次选中的子控制器的索引,默认初始化记录为0,就刚好对应默认选中0,但不来这个方法
    所以不记录上一次选中的子控制器而是记录上一次选中的子控制器的索引
    
    - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
    {
        if (tabBarController.selectedIndex == self.lastSelectedIndex) { // 重复点击了同一个TabBar按钮
            // 发出通知
            [[NSNotificationCenter defaultCenter] postNotificationName:XMGTabBarButtonDidRepeatClickNotification object:nil];
        }
        
        // 记录目前选中的索引
        self.lastSelectedIndex = tabBarController.selectedIndex;
    }
    
    

    tabbarVc切换两个子控制器,两个子控制器的View的生命周期

    ![image](images/屏幕快照 2015-12-01 16.46.16.png)

    ![image](images/屏幕快照 2015-12-01 16.46.24.png)

    • 下面输出1,3A控 ,,2,4B控,,A切B

    ![image](images/屏幕快照 2015-12-01 16.46.08.png)

    相关文章

      网友评论

          本文标题:关于UITabBar

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