在这样的情况下,我们需要用一个“壳”也就是子界面控制器来对该界面进行管理,这时候我们需要实例化一个ViewController(作为壳)。
@property(nonatomic,strong)NSArray *viewControllers;(全局变量)
{
UIScrollView *_contentView;(成员变量)//用来放置内容显示的区域
UIScrollView *_titleView;//上部可点击标题
}
//关键代码,让子界面控制器包含各个childViewController
[self.viewControllers enumerateObjectsUsingBlock:^(UIViewController *vc, NSUInteger idx, BOOL *stop) {
vc.view.frame = CGRectMake(idx * CGRectGetWidth(_contentView.frame), 0, CGRectGetWidth(_contentView.frame), CGRectGetHeight(_contentView.frame));
//添加子控制器
[self addChildViewController:vc];
[_contentView addSubview:vc.view];
//告知父视图子视图已经添加
[vc didMoveToParentViewController:self];
//自定义UIButton,使得界面随着点击而进行界面之间的切换
UIButton *btn=[UIButton buttonWithType:UIButtonTypeSystem];
[btn setTitle:vc.title forState:UIControlStateNormal];
btn.tag=idx;//用来作为切换界面的指引,切换到第几个界面
[btn addTarget:self action:@selector(change:) forControlEvents:UIControlEventTouchUpInside];
[_titleView addSubview:btn];//添加控件,自己设置范围
}];
//给contentView设定界限
_contentView.contentSize = CGSizeMake(CGRectGetWidth(self.view.bounds) * self.viewControllers.count, 0);
_titleView.contentSize ;//设定标题界限
//切换
-(void)change:(UIButton *)sender{
[_contentView setContentOffset:CGPointMake(sender.tag*CGRectGetWidth(_contentView.frame), 0)];
//[self changeController:sender.tag];
}
第一次写博客,就像做笔记一样,亦是希望能帮到想我一样的初学者