zoukankan      html  css  js  c++  java
  • iOS之自定义UITabBar替换系统默认的(添加“+”号按钮)

    自定义UITabBar替换系统默认的,目的是为了在UITabBar中间位置添加一个“+号按钮”,下面我们来聊聊具体的实现。

    1、自定义WBTabBar,让其继承自UITabBar,代码如下:

    //
    //  WBTabBar.h
    //  SinaWeibo
    //
    //  Created by android_ls on 15/5/21.
    //  Copyright (c) 2015年 android_ls. All rights reserved.
    //
    
    #import <UIKit/UIKit.h>
    
    @interface WBTabBar : UITabBar
    
    @end
    


    2、tabBar是UITabBarController的只读成员变量(属性),是不让修改的,在UITabBarController.h文件中的声明如下:

    @property(nonatomic,readonly) UITabBar *tabBar NS_AVAILABLE_IOS(3_0);

    针对于这种情况,我们可以使用KVC的方式,更换系统自带的UITabBar,实现代码如下:

        WBTabBar *tabBar = [[WBTabBar alloc] init];
        [self setValue:tabBar forKeyPath:@"tabBar"];


    3、添加一个UIButton到WBTabBar中,实现代码如下:

    - (id)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self) {
            // 添加一个按钮到tabbar中
            UIButton *plusBtn = [[UIButton alloc] init];
            [plusBtn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"] forState:UIControlStateNormal];
            [plusBtn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted];
            [plusBtn setImage:[UIImage imageNamed:@"tabbar_compose_background_icon_add"] forState:UIControlStateNormal];
            [plusBtn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateHighlighted];
            plusBtn.size = plusBtn.currentBackgroundImage.size;
            [plusBtn addTarget:self action:@selector(plusClick) forControlEvents:UIControlEventTouchUpInside];
            [self addSubview:plusBtn];
            self.plusBtn = plusBtn;
        }
        return self;
    }


    4、设置加号按钮的位置,调整WBTabBar中各个UITabBarButton的位置和宽度,具体实现代码如下:

    - (void)layoutSubviews
    {
        [super layoutSubviews];
        
        // 1.设置加号按钮的位置
        self.plusBtn.centerX = self.width * 0.5;
        self.plusBtn.centerY = self.height * 0.5;
        
        // 2.设置其它UITabBarButton的位置和尺寸
        CGFloat tabbarButtonW = self.width / 5;
        CGFloat tabbarButtonIndex = 0;
        for (UIView *child in self.subviews) {
            Class class = NSClassFromString(@"UITabBarButton");
            if ([child isKindOfClass:class]) {
                // 设置宽度
                child.width = tabbarButtonW;
                // 设置x
                child.x = tabbarButtonIndex * tabbarButtonW;
                
                // 增加索引
                tabbarButtonIndex++;
                if (tabbarButtonIndex == 2) {
                    tabbarButtonIndex++;
                }
            }
        }
    }
    


    5、定义WBTabBarDelegate协议,声明WBTabBar的代理,代码如下:

    //
    //  WBTabBar.h
    //  SinaWeibo
    //
    //  Created by android_ls on 15/5/21.
    //  Copyright (c) 2015年 android_ls. All rights reserved.
    //
    
    #import <UIKit/UIKit.h>
    
    #pragma mark 因为在UITabBar中已经声明过一个UITabBarDelegate协议,
    #pragma mark 我们若想新增一个对外的代理函数,可以让我们自定义的协议继承自UITabBarDelegate,添加一个扩展函数。
    
    @class WBTabBar;
    
    @protocol WBTabBarDelegate <UITabBarDelegate>
    
    @optional
    - (void)tabBarDidClickPlusButton:(WBTabBar *)tabBar;
    
    @end
    
    @interface WBTabBar : UITabBar
    
    @property (nonatomic, weak) id<WBTabBarDelegate> tabBarDelegate;
    
    @end
    


    6、在加号按钮的点击事件处理器中,通知代理

    #pragma mark 加号按钮点击事件处理器
    - (void)plusClick
    {
        // 通知代理
        if ([self.tabBarDelegate respondsToSelector:@selector(tabBarDidClickPlusButton:)]) {
            [self.tabBarDelegate tabBarDidClickPlusButton:self];
        }
    }


    7、在WBTabBarController中设置WBTabBar的代理,具体实现如下:

     // 2、使用KVC的方式,更换系统自带的UITabBar
        WBTabBar *tabBar = [[WBTabBar alloc] init];
        tabBar.tabBarDelegate = self;
        [self setValue:tabBar forKeyPath:@"tabBar"];
    #pragma mark - HWTabBarDelegate代理方法
    - (void)tabBarDidClickPlusButton:(WBTabBar *)tabBar
    {
        ComposeViewController *composeViewController= [[ComposeViewController alloc] init];
        UINavigationController * navigationController = [[UINavigationController alloc]initWithRootViewController:composeViewController];
        [self presentViewController:navigationController animated:YES completion:nil];
    }
  • 相关阅读:
    UVa 116 单向TSP(多段图最短路)
    POJ 1328 Radar Installation(贪心)
    POJ 1260 Pearls
    POJ 1836 Alignment
    POJ 3267 The Cow Lexicon
    UVa 1620 懒惰的苏珊(逆序数)
    POJ 1018 Communication System(DP)
    UVa 1347 旅行
    UVa 437 巴比伦塔
    UVa 1025 城市里的间谍
  • 原文地址:https://www.cnblogs.com/zhun/p/5596350.html
Copyright © 2011-2022 走看看