您的位置:首页 > 其它

自定义导航控制器的titleView实现多控制器的切换

2016-01-26 17:59 357 查看
在很多应用中都有通过自定义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];
}




内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: