自定义标签工具栏 Tabbar
来源:互联网 发布:达内编程 编辑:程序博客网 时间:2024/05/21 17:26
简单介绍一下在三级控制器中标签控制器的自定义标签工具栏。
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { _window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; _window.backgroundColor = [UIColor whiteColor]; [_window makeKeyAndVisible]; MainTabbarController *mainCtrl = [[[MainTabbarController alloc] init] autorelease]; _window.rootViewController = mainCtrl; return YES;}
MainTabbarController.h
@interface MainTabbarController : UITabBarController{ UIImageView *_selectedImg; UIView *tabbarView;}
MainTabbarController.m
(1)创建三级控制器
@implementation MainTabbarController- (void)viewDidLoad { [super viewDidLoad]; //1.创建三级控制器 [self _initViewCtrls]; //2.使用第三种自定义工具栏的方法 [self _newInitTabbar];}//1.创建视图控制器- (void)_initViewCtrls { //(1)创建视图控制器,最多只能显示5个 ProfileViewController *profileCtrl = [[[ProfileViewController alloc] init] autorelease]; GroupViewController *groupCtrl = [[[GroupViewController alloc] init] autorelease]; SearchViewController *searchCtrl = [[[SearchViewController alloc] init] autorelease]; CommentViewController *commentCtrl = [[[CommentViewController alloc] init] autorelease]; MessageViewController *messageCtrl = [[[MessageViewController alloc] init] autorelease]; //将视图控制器存放到数组中 NSArray *viewCtrls = @[profileCtrl,groupCtrl,searchCtrl,commentCtrl,messageCtrl]; //(2)创建导航控制器,并且将视图控制器交给导航控制器管理 NSMutableArray *navCtrls = [[NSMutableArray alloc] init]; for(int i=0; i<5 ; i++) { //取得视图控制器 UIViewController *viewCtrl = viewCtrls[i]; //创建导航控制器 UINavigationController *navCtrl = [[UINavigationController alloc] initWithRootViewController:viewCtrl]; [navCtrl.navigationBar setBackgroundImage:[UIImage imageNamed:@"navbar_bg_normal"] forBarMetrics:UIBarMetricsDefault]; [navCtrls addObject:navCtrl]; } //**(3)创建标签控制器** self.viewControllers = navCtrls;}
(2)使用第三种自定义工具栏的方法
- (void)_newInitTabbar { //(1)移除工具栏上的按钮 //取得tabbar上的所有子视图 NSArray *views = [self.tabBar subviews]; for (UIView *view in views) { [view removeFromSuperview]; } //(2)设置背景 self.tabBar.backgroundImage = [UIImage imageNamed:@"navbg"]; //(3)创建按钮 CGFloat width = [UIScreen mainScreen].bounds.size.width; //每一个按钮的宽度 CGFloat w = width/5; for (int i=0; i<5; i++) { UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; NSString *imageName = [NSString stringWithFormat:@"%d",i+1]; [button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal]; //设置frame button.frame = CGRectMake((w-42)/2+w*i, 2, 42, 45); //添加一个点击事件 [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside]; [self.tabBar addSubview:button]; } //(4)创建选中图片 _selectedImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"选中"]]; _selectedImg.frame = CGRectMake((w-53)/2.0, 2, 53, 45); [self.tabBar addSubview:_selectedImg];}
(3)按钮的点击事件
- (void)buttonAction:(UIButton *)button { //切换视图控制器 self.selectedIndex = button.tag; //动画方法一: [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:.2]; _selectedImg.center = button.center; [UIView commitAnimations]; //动画方法二(block): [UIView animateWithDuration:.2 animations:^{ _selecteImgView.center = button.center; } completion:^(BOOL finished) { NSLog(@"动画完成"); }];}
如果想实现一个控制器(ProfileViewController)PUSH到下一个控制器(DetailViewController)后不显示标签工具栏,则用如下方法:
(1)首先先在ProfileViewController中添加一个按钮和点击事件:
ProfileViewController.m
- (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor redColor]; //添加子视图 UIButton *button = [UIButton buttonWithType:UIButtonTypeContactAdd]; button.frame = CGRectMake(90, 90, 90, 50); [button addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button];}- (void)buttonAction { DetailViewController *detailCtrl = [[DetailViewController alloc] init]; [self.navigationController pushViewController:detailCtrl animated:YES]; [detailCtrl release];}
(2) DetailViewController.m
- (instancetype)init{ self = [super init]; if (self) { ***//注意这行代码放的位置(被push的视图控制器中)*** self.hidesBottomBarWhenPushed = YES; } return self;}
效果图如下:
0 0
- 自定义标签工具栏 Tabbar
- 自定义工具栏tabbar图片
- 自定义标签工具栏
- 创建TabBaritem和自定义Tabbar工具栏
- 两种方式自定义标签工具栏
- 两种方式自定义标签工具栏
- 自定义TabBar
- 自定义TabBar
- 自定义Tabbar
- 自定义tabbar
- 自定义tabbar
- 自定义 TabBar
- 自定义TabBar
- 自定义tabBar
- 自定义TabBar
- 自定义TabBar
- 自定义Tabbar
- 自定义tabbar
- poj 1160 post office(邮局)
- android 创建和安装OTA包
- Spring加载Hibernate映射文件最佳方式
- APP服务器端要注意的问题
- struct iphdr详解
- 自定义标签工具栏 Tabbar
- Web Service的测试需求
- GIC代码分析
- URL 和 URI概念分析
- VoIP基本概念之回声消除技术
- Ubuntu 下安装 VMware Tools 方法
- R232接口的简单介绍
- GitHub上排名前50的 Objective-C 项目
- struct tcphdr详解