自定义导航控制器的titleView实现多控制器的切换
来源:互联网 发布:如何选购基金知乎 编辑:程序博客网 时间:2024/05/18 01:53
在很多应用中都有通过自定义navigationItem的titleView,来实现多个控制器的切换管理.主要实现这个功能需要注意一下两个方面:
一,自定义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];}
1 0
- 自定义导航控制器的titleView实现多控制器的切换
- 导航控制器实现视图的分层切换
- iOS_20_微博自定义可动画切换的导航控制器
- 自定义导航控制器的动画
- 自定义控制器的切换-动画
- 导航控制器 控制视图间的切换
- segmentControl实现控制器的切换
- 【iOS开发-24】导航控制器下不同视图控制器之间切换:利用CATrasition和view的layer层来实现自定义的动画效果
- iOS开发导航控制器下不同视图控制器之间切换:利用CATrasition和view的layer层来实现自定义的动画效果
- XMG 父子控制器 自定义控制器的切换 Second
- iOS 9的自定义导航控制器
- Swift之UITabBarController 导航控制器的自定义
- 自定义导航控制器的侧滑
- 自定义导航控制器的滑动返回效果
- 导航控制器的基本概念
- 导航控制器的应用
- 导航控制器的基本概念
- 导航控制器的学习
- -bash: vim,ls: No such file or directory
- Android实现微信、QQ的程序前后台切换
- 第三课-遮罩
- JPA常用注解
- securecrt常用命令
- 自定义导航控制器的titleView实现多控制器的切换
- 移动
- Handler消息传递机制
- HTML学习笔记——(四)HTML标题、段落及文本格式化
- js 日期格式化,天数加减
- Block传值
- 云监控崛起,你落伍了么?
- Google的Gson包的使用简介
- 解决iOS键盘弹出遮挡输入框问题