如何用UIScrollView实现UIPageViewController?
我从苹果网站上拿了Photo Scroller的例子,并试图通过复制代码来实现我自己的Album。 现在, UIScrollView
不可见。 我如何让它出现?
我唯一的代码更改是在创buildUIPageViewController
。 在我的情况下,这是一个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; }
您不会将pageViewController
的视图添加为pageViewController
视图的子视图。 你的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
initWithCoder:
因为Apple的Photo Scroller代码使用了一个storyboard。 我从故事板中删除了UIPageViewController
,并在其位置创build了一个BasePhotoViewController
。 如果不从故事板加载BasePhotoViewController
,则应将initWithCoder:
的代码移到相应的初始化程序中。
编辑:看到这个示例项目在github上。
- 在iPhone上清除历史记录的有效方法
- UITplitView与UITabbar
- 如何设置iOS UITextField边框颜色? (RubyMotion + iOS)
- 无法使用UILongTouchGestureRecognizer从MKMapView获取坐标
- 在SKSpriteKit中创build可滚动表的正确方法是什么?
- 将NSDictionary传递给函数以添加对象/键但在main()中显示为空
- iOS Geofence CLCircularRegion监控。 locationManager:didExitRegion似乎没有按预期工作
- 如何将Ustreamvideoembedded到iOS中
- Firebase – 检查高效的用户名