iOS UI初级-标签控制器

来源:互联网 发布:淘宝如何延迟确认收货 编辑:程序博客网 时间:2024/05/21 17:33
1.标签控制器  UITabBarController 
UITabBarController一样是管理视图控制器的
UITabBarController是用来管理视图控制器之间的导航,UITabBarController是用来管理固定的几个视图控制器,子控制器是并列的,可以任意切换显示。
2.UITabBarController基本用法
//创建标签控制器
   
UITabBarController *tabBV = [[UITabBarControlleralloc]init];
   
//创建子控制器
   
HomeViewController *homeVC = [[HomeViewControlleralloc]init];
    homeVC.
title= @"Home";
   
MessageViewController *messageVC = [[MessageViewControlleralloc]init];
    messageVC.
title= @"Message";
   
SettingViewController *settingVC = [[SettingViewControlleralloc]init];
    settingVC.
title= @"Setting";
   
SearchViewController *searchVC = [[SearchViewControlleralloc]init];
    searchVC.
title= @"Search";
   
//创建一个数组将视图控制器装到数组中
   
NSMutableArray *arr = [NSMutableArrayarrayWithObjects:homeVC, messageVC, settingVC, searchVC,nil];
   
//创建多个控制器
   
//标签只能显示4个,第五个以上都会默认显示在表视图里边,并且所有的标签都可以更换位置
   
for (inti = 0; i < 5; i++) {
       
UIViewController *vc = [[UIViewControlleralloc]init];
       
NSString *title = [NSStringstringWithFormat:@"%d个标签", i+5];
        vc.
title= title;
        [arr
addObject:vc];
        vc.
view.backgroundColor= [UIColorcolorWithRed:arc4random() % 10* 0.1 green:arc4random() %10 * 0.1blue:arc4random() % 10* 0.1 alpha:1];
    }
    tabBV.
viewControllers= arr;
   
//进入window时,除了第一个页面的标签的title会显示出来,其他的不会显示,是因为还没有加载(生命周期)
    self.window.rootViewController = tabBV;
3.UITabBarController结构图
①一个分栏标签视图控制器控制着若干视图控制器,它是由一个数组进行管理的
②每一个分栏控制器只有一个UITabBar视图,用于显示UITabBarItem实例
③UITabBarItem由当前的视图控制器管理,这一点与导航控制器中的UIBarButtonItem是相同


4.标签控制器的常用属性和代理方法

5.自定义标签控制器
//使用自定义方式定义tabbarItem
   
UITabBarItem *item1 = [[UITabBarItemalloc]initWithTabBarSystemItem:UITabBarSystemItemFavoritestag:1];
    homeVC.tabBarItem = item1;
    //显示红色图标标记
    item1.badgeValue= @"new";

   
   
UITabBarItem *item2 = [[UITabBarItemalloc]initWithTabBarSystemItem:UITabBarSystemItemBookmarkstag:2];
    messageVC.
tabBarItem= item2;
   
   
//自定义的图片
   
UITabBarItem *item3 = [[UITabBarItemalloc]initWithTitle:@"搜索"image:[UIImageimageNamed:@"tabbar_discover.png"]selectedImage:[UIImageimageNamed:@"tabbar_discover_highlighted.png"]];
    searchVC.
tabBarItem= item3;
   
   
UITabBarItem *item4 = [[UITabBarItemalloc]initWithTitle:@"联系人"image:[UIImageimageNamed:@"tabbar_profile.png"]selectedImage:[UIImageimageNamed:@"tabbar_profile_highlighted.png"]];
    settingVC.
tabBarItem= item4;
   
    //设置tabber的背景图片
    UIImage *image = [UIImageimageNamed:@"navbg"];
   
UIGraphicsBeginImageContext(CGSizeMake([UIScreenmainScreen].bounds.size.width,49));
    [image
drawInRect:CGRectMake(0,0, [UIScreenmainScreen].bounds.size.width,49)];
    image =
UIGraphicsGetImageFromCurrentImageContext();
   
UIGraphicsEndImageContext();
    tabBV.
tabBar.backgroundImage= image;
   
   
//设置点击选中后的背景颜色
    tabBV.
tabBar.tintColor= [UIColorredColor];
   
//设置tabbar的背景颜色
    tabBV.
tabBar.barTintColor= [UIColorgrayColor];
   
//设置选中item后,显示在此item下的图片
    tabBV.tabBar.selectionIndicatorImage = [UIImageimageNamed:@"选中.png"];
6.用户定制UITabBar(二)
①//隐藏自己的tabbarView
    self.tabBar.hidden =YES;

②//先设置自定义的tabbarView的样式
   
_tabbarView = [[UIImageViewalloc]initWithFrame:CGRectMake(0, height - 49, width, 49)];
   
_tabbarView.image= [UIImageimageNamed:@"navbg.png"];
     [
self.viewaddSubview:_tabbarView];
   
//点击事件打开
    _tabbarView.userInteractionEnabled =YES;

 //设置选中的图标
    UIImage *selectedImage = [UIImageimageNamed:@"选中.png"];
   
UIImageView *selectedImageView = [[UIImageViewalloc]initWithFrame:CGRectMake(0,0,53,45)];
    selectedImageView.
image= selectedImage;
    selectedImageView.tag =100;

 ④//5个图标放到tabbar
    for (inti = 0; i < 5; i++) {
       
NSString *imageName = [NSStringstringWithFormat:@"%d.png", i+1];
       
UIButton *tabBarBtn = [UIButtonbuttonWithType:UIButtonTypeCustom];
        [tabBarBtn
setImage:[UIImageimageNamed:imageName]forState:UIControlStateNormal];
        tabBarBtn.
frame= CGRectMake(width / 5 * i, 0, width/5,49);
        tabBarBtn.
tag= 200 + i;
       
//添加点击事件
        [tabBarBtn
addTarget:selfaction:@selector(btnClick:)forControlEvents:UIControlEventTouchUpInside];
       
//第一个图标默认被选中
       
if (i == 0) {
            selectedImageView.
center= tabBarBtn.center;
        }
        [
_tabbarViewaddSubview:tabBarBtn];
       
    }
⑤将选中的按钮放在TabBar上
[_tabbarViewaddSubview:selectedImageView];

//设置选中的图标有滑动的效果
- (void)btnClick:(UIButton*)btn
{
   
//取出此标签控制器中的视图控制器
   
self.selectedIndex= btn.tag- 200;
   
UIView *selectedView = [_tabbarViewviewWithTag:100];
   
//将选中的图片放到点击的图片上
    [
UIViewanimateWithDuration:0.2animations:^{
        selectedView.
center= btn.center;
    }];
}

//创建子控制器
- (void)_initWithController
{
   
//创建子控制器
   
//三级控制器
   
HomeViewController *homeVC = [[HomeViewControlleralloc]init];
    homeVC.
title= @"Home";
   
MessageViewController *messageVC = [[MessageViewControlleralloc]init];
    messageVC.
title= @"Message";
   
SettingViewController *settingVC = [[SettingViewControlleralloc]init];
    settingVC.
title= @"Setting";
   
SearchViewController *searchVC = [[SearchViewControlleralloc]init];
    searchVC.
title= @"Search";
   
//将控制器装到数组中
   
NSArray *arr = @[homeVC, messageVC, settingVC, searchVC];
//    self.viewControllers = arr;
   
//二级控制器
   
//创建可变的数组用来装导航控制器
   
NSMutableArray *muarr = [NSMutableArrayarray];
   
for (inti = 0; i < 4; i++) {
       
//创建导航控制器
       
UINavigationController *nc = [[UINavigationControlleralloc]initWithRootViewController:arr[i]];
        [muarr
addObject:nc];
       
//取得导航控制器的代理
        nc.
delegate= self;
       
    }
   
   
//一级控制器
    self.viewControllers = muarr; 
}

//当从标签控制器控制的页面进入到导航控制器控制的页面时,将TabBar隐藏,出来时,自动恢复TabBar
#pragma mark---UINavigationControllerDelegate

- (void)navigationController:(UINavigationController*)navigationController willShowViewController:(UIViewController*)viewController animated:(BOOL)animated
{
   
CGFloat width = [UIScreenmainScreen].bounds.size.width;
   
CGFloat height = [UIScreenmainScreen].bounds.size.height;
   
//测试导航控制器中的数量
   
NSInteger count = navigationController.viewControllers.count;
   
NSLog(@"%ld", count);
   
if (count == 2) {
        [
UIViewanimateWithDuration:0.2animations:^{
           
_tabbarView.frame= CGRectMake(-width, height -49, width,49);
        }];
    }
elseif(count ==1){
        [
UIViewanimateWithDuration:0.25animations:^{
           
_tabbarView.frame= CGRectMake(0, height -49, width,49);
        }];
    }
}












































0 0
原创粉丝点击