UITabBarViewController学习笔记

ok ,今天我们学习UITabBarViewController

通过代码来UITabBarViewController。

创建一个项目,在AppDelegate.h文件的didFinishLaunchingWithOptions方法中添加下面代码。

//创建窗口

    UIWindow *window = [[UIWindowalloc]initWithFrame:[UIScreenmainScreen].bounds];

    

    //创建tabbar控制器

    UITabBarController *tabarVc = [[UITabBarControlleralloc]init];

    

    //创建子控制器

    UIViewController *vc1 = [[UIViewControlleralloc]init];

    vc1.view.backgroundColor = [UIColorgreenColor];

    //[tabarVc addChildViewController:vc1];//将子控制添加到tabbar控制器上

    vc1.tabBarItem.title =@"联系人";//设置tab的标题

    vc1.tabBarItem.badgeValue =@"10";//设置item上面的提示文字

    vc1.tabBarItem.image = [UIImageimageNamed:@"tab_buddy_nor"];//设置tab的图片

    

    

    UIViewController *vc2 = [[UIViewControlleralloc]init];

    vc2.view.backgroundColor = [UIColorredColor];

    //[tabarVc addChildViewController: vc2];

    vc2.tabBarItem.title =@"动态";

    vc2.tabBarItem.image = [UIImageimageNamed:@"tab_qworld_nor"];

    

    UIViewController *vc3 = [[UIViewControlleralloc]init];

    vc3.view.backgroundColor = [UIColoryellowColor];

    //[tabarVc addChildViewController: vc3];

    vc3.tabBarItem.title =@"设置";

    vc3.tabBarItem.image = [UIImageimageNamed:@"tab_me_nor"];


    tabarVc.viewControllers =@[vc1,vc2,vc3];

    

    

    window.rootViewController = tabarVc;

    

    [window makeKeyWindow];

    

    self.window = window;

注释已经很清楚了,我就不多说什么了。注意一下 子控制器可以 addChildViewController 一个一个添加,也可以通过viewControllers一块添加。

运行效果如下图所示:


那下面我们通过UITabBarViewController 来是实现主流的软件框架。

以新浪微博为例,先看效果图:



难点就是中间的按钮怎么去设置,因为那是一个不同按钮,不是一个UITabBarItem。 

这个地方我才去自定义TabBar的方式来实现。

1.创建一个WBTabBar,继承UITabBar。

2.创建➕号按钮的属性,并通过懒加载的方式创建➕号按钮并把它添加到TabBar上。

-(UIButton *) plusButton {

    

    if(_plusButton ==nil) {

        

        UIButton *btn = [UIButtonbuttonWithType:UIButtonTypeCustom];

        [btn setImage:[UIImageimageNamed:@"tabbar_compose_icon_add"]forState:UIControlStateNormal];

        [btn setImage:[UIImageimageNamed:@"tabbar_compose_background_icon_add"]forState:UIControlStateHighlighted];

        [btn setBackgroundImage:[UIImageimageNamed:@"tabbar_compose_button"]forState:UIControlStateNormal];

        [btn setBackgroundImage:[UIImageimageNamed:@"tabbar_compose_button_highlighted"]forState:UIControlStateHighlighted];

        

        // 默认按钮的尺寸跟背景图片一样大

        // sizeToFit:默认会根据按钮的背景图片或者image和文字计算出按钮的最合适的尺寸

        [btn sizeToFit];

        

        _plusButton = btn;

        

        [selfaddSubview:_plusButton];

    }

    

    return_plusButton;

}


3.在layoutSubviews方法中确定➕号按钮和UITabBarItem的位置。这个方法是在view的frame或bounds发生改变的时候会点用;

-(void) layoutSubviews {

    

    [superlayoutSubviews];

    

    CGFloat w =self.bounds.size.width;

    CGFloat h =self.bounds.size.height;

    

    CGFloat btnX =0;

    CGFloat btnY =0;

    CGFloat btnW = w / (self.items.count + 1);

    CGFloat btnH =self.bounds.size.height;

    

    int i =0;

    // 调整系统自带的tabBar上的按钮位置

    for (UIView *tabBarButtoninself.subviews) {

        // 判断下是否是UITabBarButton

        if ([tabBarButtonisKindOfClass:NSClassFromString(@"UITabBarButton" )]) {

            if (i ==2) {

                i = 3;

            }

            btnX = i * btnW;

            

            tabBarButton.frame =CGRectMake(btnX, btnY, btnW, btnH);

            

            i++;

        }

        

    }  

    // 设置添加按钮的位置

    self.plusButton.center =CGPointMake(w * 0.5, h *0.5);

}


4.创建 WBTabBarController 继承 UITabBarController, initialize 方法中设置item选中时的颜色。

+(void)initialize {

    // self -> WBTabBarController

    // 获取当前这个类下面的所有tabBarItem

    UITabBarItem *item = [UITabBarItemappearanceWhenContainedIn:self,nil];    

    // 创建属性字典

    NSMutableDictionary *att = [NSMutableDictionarydictionary];

    att[NSForegroundColorAttributeName] = [UIColororangeColor];    

    // 设置模型的标题属性修改控件的文字颜色

    [item setTitleTextAttributes:attforState:UIControlStateSelected]; 

}


5. viewDidLoad 方法中添加所有子控制器,并把自定义的 WBTabBar 设置给 WBTabBarController。

注意,因为UITabBarController的tabbar属性是只读的,所以通过kvc 把创建的WBTabBar属性设置给WBTabBarController

- (void)viewDidLoad {

    [superviewDidLoad];

    // Do any additional setup after loading the view.

    

    // 添加所有子控制器

    [selfsetUpAllChildViewController];

    

    // 自定义tabBar

    WBTabBar *tabBar = [[WBTabBaralloc]initWithFrame:self.tabBar.frame];

    

    // 利用KVCreadly的属性改

    [selfsetValue:tabBarforKeyPath:@"tabBar"];

}


点击下载源码












评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值