码迷,mamicode.com
首页 > 其他好文 > 详细

UITabBarController — 标签视图控制器

时间:2015-08-13 18:02:10      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:tabbar   标签视图控制器   uitabbar   

UITabBarController — 标签视图控制器

UITabBarController 分为三层结构:

(1).tab bar
(2.)Custom Content
(3.). Tab bar controller View

UITabBarController 有以下重要属性:

(1).viewControls 显示的视图控制器
(2).tabBar 标签栏
(3).delegate 代理
(4).selectedindex 选中某个tabBarItme

UITabBar

(1).tabBar是UITabBar对象,包含多个UIBarItem, 每一个tabBarItem对应一个ViewController ,tabBar的高度是49
(2).当tabBarItem超过5个时,系统会自动增加一个更多按钮,点击更多按钮,没有在底部出现的那些按钮会议列表形式显示出来

UITabBar 的属性

(1).tintColor
(2).barTintColor
(3).图像设置

tabBarItem可以设置title . image . badgeValue

可以用系统的样式创建tabBarItem

1.创建一个视图控制器对象

代码:

FirstViewController *firstVC=[[FirstViewController alloc] init];

2.创建第一个naVC

代码:

UINavigationController *firstNaVC=[[UINavigationController alloc] initWithRootViewController:firstVC];

3.创建tabbar上的按钮及其内容(这种方法是系统方法)

代码:

firstVC.tabBarItem =[[[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemHistory tag:1000] autorelease];
  (1). 按钮上添加”+99”的符号      
firstVC.tabBarItem.badgeValue =@"+99";
  (2).用自定义的方法创建:
SecondViewController *secondVC=[[SecondViewController alloc] init];
    UINavigationController *secondNaVC=[[UINavigationController alloc] initWithRootViewController:secondVC];
    secondVC.tabBarItem =[[[UITabBarItem alloc] initWithTitle:@"朋友圈" image:[UIImage imageNamed:@"缩放.png"] selectedImage:[UIImage imageNamed:@"加号.png"]] autorelease];
 (3). 创建第三个(第三种创建方法)
    ThirdViewController *thirdVC=[[ThirdViewController alloc] init];
    UINavigationController *thirdNaVC=[[UINavigationController alloc] initWithRootViewController:thirdVC];
    thirdNaVC.tabBarItem=[[[UITabBarItem alloc] initWithTitle:@"设置" image:[UIImage imageNamed:@"加号.png"] tag:1001] autorelease];

4.按钮创建好,然后创建一个UITabBarController让所有的按钮显示出来

代码:

UITabBarController *tabVC=[[UITabBarController alloc] init];

5.tabbarController 通过一个数组来管理所有要显示出来的naVC

代码:

 tabVC.viewControllers =@[firstNaVC,secondNaVC,thirdNaVC,fourNaVC,fiveNaVC,sixNaVC];
self.window.rootViewController =tabVC;

6.对tabbar进行外观设置(取消透明度)

代码:

tabVC.tabBar.translucent =NO;

7.背景颜色

代码:

tabVC.tabBar.barTiniColor =[UIColormagentaColor];

8.点击之后的选中颜色

代码:

 tabVC.tabBar.tintColor=[UIColor blackColor];

9.设置代理人

代码:

tabVC.delegate=self;

10.刚开始停留的页面下标

代码:

tabVC.selectedIndex =2;

11.方法:

-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
 设置badageValue nil 去掉全部    
viewController.tabBarItem.badgeValue=nil;
    // 或者(效果略微不同)
  @“” 还剩一个小圆点
//    viewController.tabBarItem.badgeValue=@""‘

}

12.在第一个视图中创建一个TableView

tableView的高度要 减掉tabBar的高度49 和navigationBar的高度64

13.在tableview的第二个协议中的if(!cell)cell创建中,添加一个长按手势和button

代码:

if (!cell) {
        cell=[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse] autorelease];
        //在这里创建长按手势和一个button,也是为了避免重复创建,在重复使用cell的同时,也同时使用了长安手势和button按钮
        UILongPressGestureRecognizer *longPress=[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(click:)];
        [cell addGestureRecognizer:longPress];
        [longPress release];
        UIButton *button =[UIButton buttonWithType:UIButtonTypeSystem];
        button.frame =CGRectMake(200, 20, 100, 30);
        [button setTitle:@"点击" forState:UIControlStateNormal];
        [cell addSubview:button];

    }

14.在长按手势的方法中可以进行以下操作:

代码:

-(void)click:(UILongPressGestureRecognizer *)longPress{
    NSLog(@"111");
    // 通过手势,找到手势添加的cell
    UITableViewCell *cell = (UITableViewCell *)longPress.view;
 // 创建一个快捷菜单
    UIMenuController *menu =[UIMenuController sharedMenuController];
 // 给这个快捷菜单进行定位
    [menu setTargetRect:cell.frame inView:cell.superview];
// 让菜单可以显示出来
    [menu setMenuVisible:YES animated:YES];
  // 如果想使用自定义的功能
    UIMenuItem *flag =[[UIMenuItem alloc] initWithTitle:@"测试" action:@selector(flag)];
// 把这个按钮放到快捷菜单上
    [menu setMenuItems:@[flag]];
    // 按钮如果不实现,无论系统还是自定义,如果不实现对应的方法,不会添加到快捷菜单上

}

15.快捷菜单捆绑了一个方法,这个方法必须实现,如果不实现,快捷菜单没有办法显示

代码:

-(BOOL)canBecomeFirstResponder{
    return YES;
}

16.以下系统给定的显示快捷菜单

-(void)delete:(id)sender{
    NSLog(@"删除");
}
-(void)copy:(id)sender{
    NSLog(@"复制");
}

-(void)select:(id)sender{
    NSLog(@"选择");
}

17. 也可以添加自定义

代码:

-(void)flag{
    NSLog(@"111");
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

UITabBarController — 标签视图控制器

标签:tabbar   标签视图控制器   uitabbar   

原文地址:http://blog.csdn.net/mltianya/article/details/47615917

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!