如何用 UIScrollView 实现 UIPageViewController?
Posted
技术标签:
【中文标题】如何用 UIScrollView 实现 UIPageViewController?【英文标题】:How to implement UIPageViewController with a UIScrollView? 【发布时间】:2013-06-18 14:39:31 【问题描述】:我从 Apple 的网站上获取了 Photo Scroller 示例,并尝试通过复制代码来实现我自己的相册。现在,UIScrollView
不可见。我如何让它出现?
我所做的唯一代码更改是创建UIPageViewController
。就我而言,打开它的是UIViewController
,而不是AppDelegate
。
@implementation BasePhotoViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
self = [super initWithNibName:nil bundle:nibBundleOrNil];
if (self)
// kick things off by making the first page
PhotoViewController *pageZero = [PhotoViewController photoViewControllerForPageIndex:0];
if (pageZero != nil)
// assign the first page to the pageViewController (our rootViewController)
//UIPageViewController *pageViewController = (UIPageViewController *) [[UIApplication sharedApplication] keyWindow].rootViewController;
UIPageViewController *pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:0 navigationOrientation:0 options:nil];
//UIPageViewController *pageViewController = (UIPageViewController *)self.parentViewController;
pageViewController.dataSource = self;
[pageViewController setViewControllers:@[pageZero]
direction:UIPageViewControllerNavigationDirectionForward
animated:NO
completion:NULL];
return self;
【问题讨论】:
【参考方案1】:您没有将pageViewController
的视图添加为BasePhotoViewController
的视图的子视图。你的BasePhotoViewController
类应该是这样的。注意viewDidLoad
中的代码。
BasePhotoViewController.h:
@interface BasePhotoViewController : UIViewController <UIPageViewControllerDataSource>
@property (nonatomic, strong) UIPageViewController * pageViewController;
@end
BasePhotoViewController.m:
#import "BasePhotoViewController.h"
#import "PhotoViewController.h"
@implementation BasePhotoViewController
@synthesize pageViewController;
- (id)initWithCoder:(NSCoder *)coder
self = [super initWithCoder:coder];
if (self)
PhotoViewController *pageZero = [PhotoViewController photoViewControllerForPageIndex:0];
if (pageZero != nil)
self.pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:0
navigationOrientation:0
options:nil];
self.pageViewController.dataSource = self;
[self.pageViewController setViewControllers:@[pageZero]
direction:UIPageViewControllerNavigationDirectionForward
animated:NO
completion:NULL];
return self;
- (void)viewDidLoad
[super viewDidLoad];
[self addChildViewController:self.pageViewController];
[self.view addSubview:self.pageViewController.view];
[self.pageViewController didMoveToParentViewController:self];
# pragma mark - UIPageViewControllerDataSource
- (UIViewController *)pageViewController:(UIPageViewController *)pvc viewControllerBeforeViewController:(PhotoViewController *)vc
NSUInteger index = vc.pageIndex;
return [PhotoViewController photoViewControllerForPageIndex:(index - 1)];
- (UIViewController *)pageViewController:(UIPageViewController *)pvc viewControllerAfterViewController:(PhotoViewController *)vc
NSUInteger index = vc.pageIndex;
return [PhotoViewController photoViewControllerForPageIndex:(index + 1)];
@end
注意:我在initWithCoder:
中初始化了UIPageViewController
,因为Apple 的Photo Scroller 代码使用故事板。我从情节提要中删除了UIPageViewController
,并在其位置创建了BasePhotoViewController
。如果您没有从情节提要加载BasePhotoViewController
,则应将initWithCoder:
中的代码移动到适当的初始化程序。
编辑:请参阅 github 上的 sample project。
【讨论】:
以上是关于如何用 UIScrollView 实现 UIPageViewController?的主要内容,如果未能解决你的问题,请参考以下文章
如何用每页不同的颜色更改 UIPageControl 中分页点的颜色
脑筋急转弯:如何用两个栈实现一个队列 && 如何用两个队列实现一个栈