先上截图:
demo地址:MultiplePagesViewControllerDemo
demo解释
首先,创建MultiplePagesViewController
的实例,并将其和其view加入到需要的view controller 中:
1
2
3
|
[self.view addSubview:self.multiplePagesViewController.view];
[self addChildViewController:self.multiplePagesViewController];
|
其中,self.multiplePagesViewController
对应的getter函数如下:
1
2
3
4
5
6
7
8
9
10
|
- (MultiplePagesViewController*)multiplePagesViewController {
if (!_multiplePagesViewController) {
_multiplePagesViewController = [[MultiplePagesViewController alloc] init];
_multiplePagesViewController.view.frame = self.view.frame;
_multiplePagesViewController.delegate = self;
}
return _multiplePagesViewController;
}
|
并且,在viewDidLayoutSubviews
中重新调整view的大小,以解决转屏等问题:
1
2
3
4
5
6
|
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
self.multiplePagesViewController.view.frame = self.view.frame;
}
|
然后,只需要将你的UIViewController的实例加入self.multiplePagesViewController
就可以了:
1
2
3
4
5
6
7
8
|
- (void)addDefaultPageViewControllers {
NSInteger vcCount = 3;
for (NSInteger i = 0; i < vcCount; i++) {
SampleViewController *svc = [[SampleViewController alloc] initWithText:[NSString stringWithFormat:@"%ld", i]];
[self.multiplePagesViewController addViewController:svc];
}
}
|
MultiplePagesViewController
主要的两个接口如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
/**
* add a new <UIViewController*> instance
*
* @param viewController
*/
- (void)addViewController:(UIViewController*)viewController;
/**
* remove an existing view controller by index
*
* @param viewControllerIndex
*/
- (void)removeViewController:(NSUInteger)viewControllerIndex;
|
另外,可以在MultiplePagesViewController
的父view controller中实现下面接口,当切换page的时候做些其它事情,比如更新view controller的title信息等。
1
2
3
4
5
6
|
@protocol MultiplePagesViewControllerDelegate <NSObject>
- (void)pageChangedTo:(NSInteger)pageIndex;
@end
|