控制器 ------ tabbar控制器 和 导航控制器

来源:互联网 发布:java 网络编程笔试题 编辑:程序博客网 时间:2024/05/17 04:47

1.自定义一个底部 tabbarController ,然后自定义一个tabbarbutton  ,继承UITabBar, 在控制器中使用kvc模式拿到UITabBar;

1》启动控制器 tabbar

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

    

    XBTabBarController *tab = [[XBTabBarControlleralloc]init];

    window.rootViewController = tab;

    

   self.window = window;

    [self.windowmakeKeyAndVisible];

2》在tabbarController中 拿到tabbar

  XBTabBar *tab = [[XBTabBaralloc]init];

    //使用KVC模式设置tabbar

    [selfsetValue:tab forKeyPath:@"tabBar"];

3》tabbar中定义tabbar的按钮位置,注意要在- (void)layoutSubviews中进行位置处理。

- (void)layoutSubviews{

    [superlayoutSubviews];

    //设置+号按钮的位置

    

    //self.plusbtn.center = CGPointMake(self.width * 0.5, self.height * 0.5);

    

    

    //设置其他tabbarbutton的位置

   CGFloat tabbarbuttonW  =self.frame.size.width /4;

   CGFloat tabbatbuttonIndex = 0;

   for ( UIView *childin self.subviews) {

        Class class =NSClassFromString(@"UITabBarButton");

       if ([child isKindOfClass:class]) {

            

           // 设置宽度

           CGFloat witch = tabbarbuttonW;

           // 设置x

         CGFloat X = tabbatbuttonIndex * tabbarbuttonW;

            

            child.frame =CGRectMake(X, 0, witch,self.frame.size.height);

            

           // 增加索引

            tabbatbuttonIndex++;

        }

    }

}

4》tabbar系统会自动进行渲染,要在进行创建的时候进行禁止渲染

//  设置vcTabBarItem

- (void)setUpController:(UIViewController*)vcClack  title:(NSString*)title imageName:(NSString*)imageName SelecdImage:(NSString*)SelecdImage{

    vcClack.title = title;

    vcClack.tabBarItem.image = [UIImageimageNamed:imageName];

    UIImage * meImage = [UIImageimageNamed:SelecdImage];

    vcClack.tabBarItem.selectedImage = [meImageimageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    XBNavigationController *nav = [[XBNavigationControlleralloc]initWithRootViewController:vcClack];

    [selfaddChildViewController:nav];

}



0 0