UITabBarController — 标签视图控制器

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个时,系统会自己主动添加一个很多其它button,点击很多其它button,没有在底部出现的那些button会议列表形式显示出来

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上的button及其内容(这样的方法是系统方法)

代码:

firstVC.tabBarItem =[[[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemHistory tag:1000] autorelease];
  (1). button上加入”+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.button创建好,然后创建一个UITabBarController让全部的button显示出来

代码:

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的同一时候,也同一时候使用了长安手势和buttonbutton
        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)];
// 把这个button放到快捷菜单上
    [menu setMenuItems:@[flag]];
    // button假设不实现,不管系统还是自己定义,假设不实现相应的方法,不会加入到快捷菜单上

}

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");
}
posted @ 2017-08-11 09:54  mfmdaoyou  阅读(309)  评论(0编辑  收藏  举报