自定义导航控制器的titleView实现多控制器的切换
2016-01-26 17:59
357 查看
在很多应用中都有通过自定义navigationItem的titleView,来实现多个控制器的切换管理.主要实现这个功能需要注意一下两个方面:
一,自定义titleView,里面包含两个按钮,监听点击事件,切换到哪个控制器
二,scrollView确定滚动的范围,有几个控制器就设置contentSize有多大
三,切换的控制器view,添加到scrollView上,使得在滚动的时候可以切换控制器
四,设置当前的控制的addChildViewController为要切换的控制器
五,实现scrollView的代理方法,当结束拖动的时候,重新给titleView的下面的滑动条设置位置
代码如下:
![](http://img.blog.csdn.net/20160126175855723?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
一,自定义titleView,里面包含两个按钮,监听点击事件,切换到哪个控制器
二,scrollView确定滚动的范围,有几个控制器就设置contentSize有多大
三,切换的控制器view,添加到scrollView上,使得在滚动的时候可以切换控制器
四,设置当前的控制的addChildViewController为要切换的控制器
五,实现scrollView的代理方法,当结束拖动的时候,重新给titleView的下面的滑动条设置位置
代码如下:
#import "ViewController.h" #import "WTNavViewController.h" #import "WTOneViewController.h" #import "WTTwoViewController.h" /**获取屏幕的宽度NSInteger*/ #define CurrentScreenWidth [UIScreen mainScreen].bounds.size.width /**获取屏幕的高度NSInteger*/ #define CurrentScreenHeight [UIScreen mainScreen].bounds.size.height @interface ViewController ()<UIScrollViewDelegate> @property (nonatomic,strong) UIView *topBgView; @property (nonatomic,strong) UIScrollView *scrollView; @property (nonatomic,strong) WTTwoViewController *two; @property (nonatomic,strong) WTOneViewController *one; @end @implementation ViewController /** * 初始化控制器 */ -(WTTwoViewController *)two { if (_two == nil) { _two = [[WTTwoViewController alloc] init]; _two.view.frame = CGRectMake(CurrentScreenWidth, 0, CurrentScreenWidth, CurrentScreenHeight); _two.view.clipsToBounds = YES; } return _two; } -(WTOneViewController *)one { if (_one == nil) { _one = [[WTOneViewController alloc] init]; _one.view.frame = CGRectMake(0, 0, CurrentScreenWidth, CurrentScreenHeight); _one.view.clipsToBounds = YES; } return _one; } - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor whiteColor]; //创建中间文字 [self createTitleView]; //创建scrollView [self createScrollView]; } /** * 创建scrollView */ -(void)createScrollView { self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, CurrentScreenWidth, CurrentScreenHeight )]; self.scrollView.contentSize = CGSizeMake(CurrentScreenWidth * 2, CurrentScreenHeight); self.scrollView.showsVerticalScrollIndicator = NO; self.scrollView.showsHorizontalScrollIndicator = NO; self.scrollView.bounces = NO; self.scrollView.pagingEnabled = YES; [self.view addSubview:self.scrollView]; self.scrollView.delegate = self; //注册控制器 [self configSonViewController]; } /** * 创建中间文字 */ -(void)createTitleView { self.topBgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 120, 26)]; self.topBgView.userInteractionEnabled = YES; NSArray *titleArray = @[@"IOS",@"安卓"]; int i = 0; for (NSString *title in titleArray) { UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; btn.frame = CGRectMake(i * 53, 0, 58, 26); [btn setTitle:title forState:UIControlStateNormal]; btn.tag = i; [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted]; [btn addTarget:self action:@selector(titleViewSele:) forControlEvents:UIControlEventTouchUpInside]; [self.topBgView addSubview:btn]; i++; } UIView *indView = [[UIView alloc] initWithFrame:CGRectMake(14, 30, 30, 2)]; indView.backgroundColor = [UIColor redColor]; indView.tag = 99; [self.topBgView addSubview:indView]; self.navigationItem.titleView = self.topBgView; } /** * 选择哪个控制器 */ -(void)titleViewSele:(UIButton *)btn { NSInteger ss = 0; switch (btn.tag) { case 0: { ss = 14; self.scrollView.contentOffset = CGPointMake(0, 0); } break; case 1: { ss = 65; self.scrollView.contentOffset = CGPointMake(CurrentScreenWidth, 0); } default: break; } UIView *indView = (UIView *)[_topBgView viewWithTag:99]; CGRect inFrame = indView.frame; inFrame.origin.x = ss; [UIView animateWithDuration:0.2 animations:^{ indView.frame = inFrame; }]; } #pragma mark - 代理方法 -(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { NSInteger ss = 0; if (self.scrollView.contentOffset.x == 0) { ss = 14; }else{ ss = 65; } UIView *indView = (UIView *)[_topBgView viewWithTag:99]; CGRect inFrame = indView.frame; inFrame.origin.x = ss; [UIView animateWithDuration:0.2 animations:^{ indView.frame = inFrame; }]; } /** * 注册控制器 */ -(void)configSonViewController { [self.scrollView addSubview:self.one.view]; [self.scrollView addSubview:self.two.view]; [self addChildViewController:self.one]; [self addChildViewController:self.two]; }
相关文章推荐
- HTML学习笔记——(四)HTML标题、段落及文本格式化
- Fragment流程图
- JPA常用注解
- Spring 3.0 AOP (一)AOP 术语
- 电信、网通、联通等恶意DNS劫持跳广告页面的解决方法
- java多线程之内存可见性学习笔记
- 【Linux/OS/Network】匿名管道(pipe)和命名管道(FIFO)
- Android实现微信、QQ的程序前后台切换
- Android的EditText在怎样获取焦点并弹出软键盘
- @Resource注解
- codeforces 29D Ant on the Tree (dfs,tree,最近公共祖先)
- android方法名超过65k的问题
- Java并发编程:Callable、Future
- java抓取有验证的页面内容
- spring有办法在getBean的时候传入构造函数的参数吗?
- Android客户端采用RestTemplate实现附件的上传
- 关于js中原型链的理解
- 如何设置jvm的内存
- LAMP之二(PHP-FPM)
- hibernate jpa 注解 @Temporal()