iOS UITabBarController使用方法(欢迎提建议)

来源:互联网 发布:cc域名转入 编辑:程序博客网 时间:2024/06/06 11:00


UITabBarController系统自带控件的使用方法:


首先,创建一个demo ,然后:

1. 创建一个继承于 UITabBarController 的类   

 

BaseTabBarViewController.h


#import <UIKit/UIKit.h>


@interface BaseTabBarViewController :UITabBarController


@end




BaseTabBarViewController.m

 

@implementation BaseTabBarViewController


- (void)viewDidLoad {

    [superviewDidLoad];

    // Do any additional setup after loading the view.

    

    //设置tabBar背景图片

    

    self.tabBar.backgroundImage = [UIImageimageNamed:@"tabbarBgDiv.jpg"];

    

    //加载TabBarController控制器类

    

    [selfinitTabBarViewControllors];

}


- (void)initTabBarViewControllors

{

    //初始化

    APengViewController *apengVC = [[APengViewController alloc]init];

    

    LoginViewController *loginVC = [[LoginViewControlleralloc]init];

    

   NSArray *rootControllers =@[apengVC,apengVC,loginVC];

   //将每个标签栏名字存入数组

   NSArray *rootTabBarNames =@[@"标签1",@"标签2",@"标签3"];

    

   //指定数组的数量

   NSMutableArray *viewControllers = [[NSMutableArrayalloc]initWithCapacity:4];

    

    

   //循环设置每个对应的标签栏标题和图片

   for(int i =0 ; i < [rootTabBarNamescount]; i++)

    {

       UINavigationController *navigationViewController = [[UINavigationControlleralloc]initWithRootViewController:[rootControllersobjectAtIndex:i]];


        navigationViewController.tabBarItem.title = [rootTabBarNamesobjectAtIndex:i];

        

       //定义两张图片,一张选中时,一张为选中时

       UIImage *selectedImage = [UIImageimageNamed:[NSStringstringWithFormat:@"tabbar_select_%d.png",i]];

        

       UIImage *unSelectedImage = [UIImageimageNamed:[NSStringstringWithFormat:@"tabbar_normal_%d.png",i]];

        

        [navigationViewController.tabBarItemsetFinishedSelectedImage:selectedImagewithFinishedUnselectedImage:unSelectedImage];

        

        [viewControllersaddObject:navigationViewController];

    }

    

   self.viewControllers = viewControllers;

    

}


- (void)didReceiveMemoryWarning {

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}



@end






最后在appdelegate.m里的:


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    // Override point for customization after application launch.

    

    BaseTabBarViewController *baseVC = [[BaseTabBarViewControlleralloc]init];

   //默认设置索引为1的标签控制器

    baseVC.selectedIndex =1;

    self.window.rootViewController = baseVC;

    

    returnYES;

}


做ios 也有些日子了,但是好多细节的东西还是没有搞明白,反思了下主要是太过浮躁.... 希望以后能静心修炼 。保持饥饿,保持愚蠢。





实现是可以实现的,但是我这里有个问题: 为什么要用 UINavigationController 呢? 哪位高手给解释下啊委屈

0 0
原创粉丝点击