UIPageViewController子视图控制器之间的水平填充

我正在使用UIPageViewController来显示嵌入在子视图控制器中的图像。

NSDictionary *options = [NSDictionary dictionaryWithObject: [NSNumber numberWithInteger:UIPageViewControllerSpineLocationMin] forKey: UIPageViewControllerOptionSpineLocationKey]; self.pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options: options]; self.pageViewController.dataSource = self; self.pageViewController.view.frame = self.view.bounds; ImageViewController *initialViewController = [self viewControllerAtIndex:0]; initialViewController.index = 0; NSArray *viewControllers = [NSArray arrayWithObject:initialViewController]; [self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil]; [self addChildViewController:self.pageViewController]; [self.view addSubview:self.pageViewController.view]; [self.pageViewController didMoveToParentViewController:self]; 

一切都很好,但我讨厌儿童视图控制器彼此相邻。

在此处输入图像描述

我想知道是否有办法在子视图控制器之间添加填充,这样它们就不会彼此相邻。

看起来更像是这样的东西

在此处输入图像描述

检查UIPageViewController init方法

- (id)initWithTransitionStyle:(UIPageViewControllerTransitionStyle)style navigationOrientation:(UIPageViewControllerNavigationOrientation)navigationOrientation options:(NSDictionary *)opt

您可以传递您的页间空间值以opt UIPageViewControllerOptionInterPageSpacingKey

故事板中选择UIPageViewController 。 在Attributes Inspector中,有一个用于设置Page Spacing的选项。

使用Swift,您将使用initWithTransitionStyle:navigationOrientation:options: initializer并将值传递给options参数中的UIPageViewControllerOptionInterPageSpacingKey以在页面之间设置空格。

作为示例,以下代码显示了如何使用Swift 2.2实现它:

 let optionsDict = [UIPageViewControllerOptionInterPageSpacingKey : 20] let pageViewController = UIPageViewController(transitionStyle: .Scroll, navigationOrientation: .Horizontal, options: optionsDict) 

请注意, UIPageViewController类引用声明了UIPageViewControllerOptionInterPageSpacingKey

该值应该是包含在NSNumber实例中的CGFloat。

但是,如前面的代码所示,类型为[String : Int]的Dictionary也有效。