在開發(fā)中經(jīng)常會(huì)用頭部標(biāo)題+底部輪播或者頁(yè)面轉(zhuǎn)換唉擂,類似于頭條效果夭织,此時(shí)不能在UIScrollView+UITableView混合使用网杆,如果使用UItableView的話吃环,加載數(shù)據(jù)時(shí)滑動(dòng)會(huì)導(dǎo)致程序崩潰也颤,所以需要使用addChildViewController方法;
這里寫一個(gè)簡(jiǎn)單的實(shí)例郁轻,標(biāo)題欄僅寫兩個(gè)
總體布局為頭部UIView上添加button翅娶,底部添加scrollView,scrollView上添加子控制器好唯。
準(zhǔn)備工作竭沫,新建兩個(gè)ViewController:TestOneVC、TestTwoVC
1)新建UIView骑篙,命名為kndView蜕提;新建兩個(gè)button:one、two靶端;新建一個(gè)scrollView谎势;
@property(nonatomic,strong)UIView *kindView;
@property(nonatomic,strong)UIButton *one;
@property(nonatomic,strong)UIButton *two;
@property(nonatomic,strong)UIScrollView *scrollView;
2)初始化kndView、one杨名、two脏榆、scrollView
self.kindView = [[UIView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_W, 44)];
self.kindView.backgroundColor = [UIColor colorWithHexString:@"FFFFFF"];
[self.view addSubview:self.kindView];
self.one = [UIButton buttonWithType:(UIButtonTypeCustom)];
[self.one setTitle:@"一" forState:(UIControlStateNormal)];
[self.one addTarget:self action:@selector(oneAction) forControlEvents:(UIControlEventTouchUpInside)];
self.one.frame = CGRectMake(0, 0, SCREEN_W/2, 44);
[self.kindView addSubview:self.one];
[self.one setTitleColor:[UIColor colorWithHexString:@"030303"] forState:(UIControlStateNormal)];
self.one.titleLabel.font = [UIFont systemFontOfSize:18];
self.two = [UIButton buttonWithType:(UIButtonTypeCustom)];
[self.two setTitle:@"er" forState:(UIControlStateNormal)];
[self.two addTarget:self action:@selector(twoAction) forControlEvents:(UIControlEventTouchUpInside)];
self.two.frame = CGRectMake(SCREEN_W/2, 0, SCREEN_W/2, 44);
[self.kindView addSubview:self.two];
[self.two setTitleColor:[UIColor colorWithHexString:@"999999"] forState:(UIControlStateNormal)];
self.two.titleLabel.font = [UIFont systemFontOfSize:16];
self.scrollView = [[UIScrollView alloc] init];
[self.view addSubview:self.scrollView];
self.scrollView.frame = CGRectMake(0, CGRectGetMaxY(self.kind.frame), SCREEN_W, SCREEN_H-64-44-44);
self.scrollView.delegate = self;
self.scrollView.contentSize = CGSizeMake(SCREEN_W*2, 0);
self.scrollView.bounces = NO;
self.scrollView.pagingEnabled = YES;
self.scrollView.showsHorizontalScrollIndicator = NO;
self.scrollView.scrollEnabled = NO;
self.scrollView.delegate = self;
3)添加第一個(gè)子控制器
TestOneVC *one =? [[TestOneVC alloc] init];
[self.scrollView addSubview:one.view];
[self addChildViewController:one];
[self.view insertSubview:self.scrollView belowSubview:self.kindView];
4)創(chuàng)建剩余的子控制器
- (void)setupChildViewController{
TestTwoVC *two = [[TestTwoVC alloc] init];
[self addChildViewController:two];
}
5)button方法+子控制器選擇方法
- (void)showVc:(NSInteger)index {
CGFloat offsetX = index * SCREEN_W;
UIViewController *vc = self.childViewControllers[index];
// 判斷控制器的view有沒(méi)有加載過(guò),如果已經(jīng)加載過(guò),就不需要加載
if (vc.isViewLoaded) return;
[self.backScroll addSubview:vc.view];
vc.view.frame = CGRectMake(offsetX, 0, self.view.frame.size.width, self.view.frame.size.height);
}
-(void)oneAction{
self.scrollView.contentOffset = CGPointMake(0, 0);
[self.one setTitleColor:[UIColor colorWithHexString:@"030303"] forState:(UIControlStateNormal)];
self.one.titleLabel.font = [UIFont systemFontOfSize:18];
[self.two setTitleColor:[UIColor colorWithHexString:@"999999"] forState:(UIControlStateNormal)];
self.two.titleLabel.font = [UIFont systemFontOfSize:16];
[self showVc:0];
}
-(void)twoAction{
[self showVc:1];
self.backScroll.contentOffset = CGPointMake(SCREEN_W, 0);
[self.one setTitleColor:[UIColor colorWithHexString:@"999999"] forState:(UIControlStateNormal)];
self.one.titleLabel.font = [UIFont systemFontOfSize:16];
[self.two setTitleColor:[UIColor colorWithHexString:@"030303"] forState:(UIControlStateNormal)];
self.two.titleLabel.font = [UIFont systemFontOfSize:18];
}
6)如果想要滑動(dòng),在scrollView的代理方法中添加一下方法:
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
if (scrollView.contentOffset.x/SCREEN_W==0) {
[self.one setTitleColor:[UIColor colorWithHexString:@"030303"] forState:(UIControlStateNormal)];
elf.one.titleLabel.font = [UIFont systemFontOfSize:18];
self.two.titleLabel.font = [UIFont systemFontOfSize:16];
[self showVc:0];
}else{
[self.one setTitleColor:[UIColor colorWithHexString:@"999999"] forState:(UIControlStateNormal)];
self.one.titleLabel.font = [UIFont systemFontOfSize:16];
[self.two setTitleColor:[UIColor colorWithHexString:@"030303"] forState:(UIControlStateNormal)];
self.two.titleLabel.font = [UIFont systemFontOfSize:18];
[self showVc:1];
}
}
7)細(xì)節(jié)處理
self.automaticallyAdjustsScrollViewInsets = NO;//不讓view亂跑
TestOneVC *one = [[TestOneVC alloc] init];
[self.scrollView addSubview:one.view];//添加one的view控制器到scrollView上台谍,已經(jīng)添加過(guò)了须喂,所以創(chuàng)建子控制器時(shí)不需要再添加這一個(gè)了
[self addChildViewController:one];//添加子控制器
[self.view insertSubview:self.scrollView belowSubview:self.kindView];將self.scrollView添加在kindView的下邊