对UITabBarController的理解

来源:互联网 发布:simlab软件敏感性分析 编辑:程序博客网 时间:2024/06/06 17:42

理解:和UINavigationController一样,UITabBarController也可以控制多个页面导航。用户可以在多个视图控制器之间移动,并可以定制屏幕底部的选项卡栏。

    UITabBarController是组建一系列的控制器,可以是UIViewController,UITableViewController,UINavigationController等,并将它们添加到选项卡栏,使每一个选项卡对应一个控制器。UINavigationController是以栈的形式将视图推入推出。UITabbarController一般作为appDelegate的根rootController使用。

分析图:


  事例:
.h中,

方法一只能适用于iOS5以下的系统,在iOS5+上面无法使用,所以必须把这两种方法结合起来,当使用viewController.hidesBottomBarWhenPushed = YES,然后再加上@interface TabBarAppDelegate :UIResponder <UIApplicationDelegate>


@property (strong, nonatomic) UIWindow *window;

@property (strong, nonatomic) TabBarViewController * tabbarController;

.m中,

    AViewController *aController = [[AViewControlleralloc] init];

    UINavigationController *nav = [[UINavigationControlleralloc] initWithRootViewController:aController];

     //aController.title = @"aa";

    BViewController *bController = [[BViewControlleralloc] init];

   // bController.title = @"bb";

    CViewController *cController = [[CViewControlleralloc] init];

   // cController.title = @"cc";

    DViewController *dController = [[DViewControlleralloc] init];

   // dController.title = @"dd";

 _tabbarController = [[TabBarViewControlleralloc] initWithNibName:@"TabBarViewController"bundle:nil];

    _tabbarController.viewControllers = [NSArrayarrayWithObjects:nav,bController,cController,dController,nil];

 //设置tabbar

//适合ios5以下

UIImageView *tabbarGroundImage = [[UIImageViewalloc] initWithImage:[UIImageimageNamed:@"mainbar_bg.png"]];

    [tabbarGroundImage setFrame:CGRectMake(0,0,self.tabBar.frame.size.width,self.tabBar.frame.size.height)];

    //ios5以下使用

    [self.tabBar insertSubview:tabbarGroundImage atIndex:0];

    [tabbarGroundImage release];


   //适和ios5以上

    [_tabbarController.tabBarsetBackgroundImage:[UIImageimageNamed:@"mainbar_bg.png"]];

    UITabBarItem *aTabbar = [[UITabBarItemalloc] initWithTitle:nilimage:[UIImageimageNamed:@"mainbar_icon1_n.png"]tag:0];

     [aTabbar setFinishedSelectedImage:[UIImageimageNamed:@"mainbar_icon1_d.png"]withFinishedUnselectedImage:[UIImageimageNamed:@"mainbar_icon1_h.png"]];

    aController.tabBarItem = aTabbar;

    [aTabbar release];

    

    [aController release];

    [nav release];

    [bController release];

    [cController release];

    [dController release];

    self.window.rootViewController =_tabbarController;


注:用TabbarController上的tabbar,backGroundImage和tabbarItem上的image都是不能改变大小的,所以对切图要求很高。
//push到第二页的时候需要隐藏掉tabbar。方法:

 EViewController *eController = [[EViewControlleralloc] initWithNibName:@"EViewController"bundle:nil];

//在push之前加上这句话,放可管用。

     eController.hidesBottomBarWhenPushed =YES;

    [self.navigationControllerpushViewController:eController animated:YES];

    [eController release];






总结:

原创粉丝点击