day8---UITabBarController

一 分栏控制器 UITabBarController
分栏控制器和导航控制器都是用来管理视图控制器的

导航控制器是用来管理视图控制器之间的导航,视图控制器之间的层递关系,一级-》二级-》三级。。

分栏控制器是管理固定的几个视图控制器,子视图控制器之间是并列关系(平级关系),可以任意切换显示;
分栏控制器可以管理导航控制器  ;
导航控制器不能管理分栏控制器;
【Demo】-【1-UITabBarController】
设计4个导航控制器,用分栏控制器管理
  • (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];

    ViewController1 *vc1 = [[ViewController1 alloc] init];
    // vc1.title = @"主页";
    // vc1.tabBarItem.image = [UIImage imageNamed:@"tab_0"];
    UITabBarItem *item1 = [[UITabBarItem alloc] initWithTitle:@"主页" image:[UIImage imageNamed:@"tab_0"] tag:100];
    vc1.tabBarItem = item1;

    UINavigationController *nvc1 = [[UINavigationController alloc] initWithRootViewController:vc1];

ViewController2 *vc2 = [[ViewController2 alloc] init];

// vc2.title = @"页面二";
// vc2.tabBarItem.image = [UIImage imageNamed:@"tab_1"];
UITabBarItem *item2 = [[UITabBarItem alloc] initWithTitle:@"页面二" image:[UIImage imageNamed:@"tab_1"] selectedImage:[UIImage imageNamed:@"tab_2"]];
vc2.tabBarItem = item2;

UINavigationController *nvc2 = [[UINavigationController alloc] initWithRootViewController:vc2];


ViewController3 *vc3 = [[ViewController3 alloc] init];

// vc3.title = @"页面三";
// vc3.tabBarItem.image = [UIImage imageNamed:@"tab_2"];
UITabBarItem *item3 = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemDownloads tag:101];
vc3.tabBarItem = item3;

UINavigationController *nvc3 = [[UINavigationController alloc] initWithRootViewController:vc3];



ViewController4 *vc4 = [[ViewController4 alloc] init];
vc4.title = @"页面四";
UINavigationController *nvc4 = [[UINavigationController alloc] initWithRootViewController:vc4];
vc4.tabBarItem.image = [UIImage imageNamed:@"tab_3"];




//*************** 创建分栏控制器对象 *************//
UITabBarController *tabVC = [[UITabBarController alloc] init];
//把4个导航控制器对象给tabVC管理
tabVC.viewControllers = @[nvc1,nvc2,nvc3,nvc4];
//设置修饰图片
tabVC.tabBar.selectionIndicatorImage = [UIImage imageNamed:@"tab_s"];
//设置tabBar上内容的渲染颜色
tabVC.tabBar.tintColor = [UIColor redColor];

//把tabBar设置为窗口的跟视图控制器
tabVC.tabBar.barStyle = UIBarStyleBlack;
//设置是否透明
tabVC.tabBar.translucent = NO;
//根据设定的值来开启初始界面
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *str = [defaults objectForKey:@"下标"];
int index = str.intValue;
tabVC.selectedIndex = index;

tabVC.delegate = self;
//把tabVC设置为窗口的根视图控制器
self.window.rootViewController = tabVC;

return YES;

}

二 NSUserDefaults
数据本地存储的方式:
1.plist文件
2.归档(存储自定义的对象,对象要遵守NSCoding协议)
3.文件管理NSFileHandle,NSFileManager
4.NSUserDefaults 单例类
把内存中的数据存储到本地,适合于存储一些小型数据,比如:用户名,密码的保存;播放记录,收藏记录;
5.数据库(存储相比较大一些的数据)

pragma mark -UITabBarControllerDelegate

//当点击tabBarItem的时候,会响应该方法
-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{

NSLog(@"%ld",tabBarController.selectedIndex);
int index = (int)tabBarController.selectedIndex;
NSString *str = [NSString stringWithFormat:@"%d",index];

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:str  forKey:@"下标"];
[defaults synchronize];

}

三 如何自定义UITabBar 【开发中最常用】

步骤:
1.隐藏系统自带的tabBar;
2.根据需求自己添加背景视图(frame设置在tabBar的位置);
3.根据项目有需求添加多个按钮/label/UIView......,实现界面的切换
见【Demo】-【2-CustomTabBar】
  • (void)viewDidLoad {
    [super viewDidLoad];

    NSMutableArray *mArr = [NSMutableArray array];
    for (int i=0; i<4; i++) {
    //循环拿到类名所对应的字符串
    NSString *className = [NSString stringWithFormat:@"ViewController%d",i+1];

      //通过字符串反射出对应的类对象
      Class class = NSClassFromString(className);
      //通过类对象创建类的对象
      UIViewController *vc = [[class alloc] init];
      UINavigationController *nvc = [[UINavigationController alloc] initWithRootViewController:vc];
      vc.title = [NSString stringWithFormat:@"界面%d",i+1];
      
      //把4个导航控制器循环依次加入到可变数组中保存起来
      [mArr addObject:nvc];
    

    }

    //把这个数组mArr给标签控制器所管理
    self.viewControllers = mArr;

//自定义tabBar

[self customTabBar];

}

-(void)customTabBar{

/*********** 1.隐藏系统自带的tarBar *************/
self.tabBar.hidden = YES;

/*********** 2.根据项目需求添加背景图片 ************/
UIImageView *bgImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, screenBounds.size.height-49, screenBounds.size.width, 49)];

// bgImageView.alpha = 0.7;
bgImageView.image = [UIImage imageNamed:@"tabbg"];
bgImageView.userInteractionEnabled = YES;
[self.view addSubview:bgImageView];

/********** 3.根据需求添加多个按钮或者label **********/
_btnArray = [NSMutableArray array];
CGFloat wSpace = (screenBounds.size.width-120)/5;

for (int i=0; i<4; i++) {
    UIButton *btn = [UIButton buttonWithType: UIButtonTypeCustom];
    btn.frame = CGRectMake(wSpace+i*(30+wSpace), 2, 30, 30);
    //在正常状态下显示的背景图片
    [btn setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"tab_%d",i]] forState:UIControlStateNormal];
    //在选中状态下显示的背景图片
    [btn setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"tab_c%d",i]] forState:UIControlStateSelected];
    
    if (i==0) {
        btn.selected = YES;
    }
    
    btn.tag = 100+i;
    //添加点击事件
    [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [bgImageView addSubview:btn];
    [_btnArray addObject:btn];
}

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(wSpace, 34, 30, 2)];
label.backgroundColor = [UIColor yellowColor];
label.tag = 200;
[bgImageView addSubview:label];

}

-(void)btnClick:(UIButton *)btn{
NSLog(@"%ld",btn.tag);

UILabel *label = (UILabel *)[self.view viewWithTag:200];
[UIView animateWithDuration:0.5 animations:^{
    label.frame = CGRectMake(btn.frame.origin.x, 34, 30, 2);
}];


//@[btn1,btn2,btn3,btn4];
for (UIButton *button in _btnArray) {
    if (button.tag == btn.tag) {
        //让当前点击的那个按钮处于选中状态
        btn.selected = YES;
    }else
    {
        //让其他的按钮处于非选中状态
        button.selected = NO;
    }
}




//**************** 4.根据点击的button实现页面的切换 **************************
self.selectedIndex = btn.tag - 100;

你可能感兴趣的:(day8---UITabBarController)