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

13

我正在使用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];

一切都很好,但我讨厌子视图控制器彼此挨着。

enter image description here

我想知道是否有一种方法可以在子视图控制器之间添加填充,以便它们不会挨在一起。

类似这样的效果:

enter image description here

3个回答

18

检查UIPageViewController的初始化方法

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

您可以通过将页面间距值传递给opt中的UIPageViewControllerOptionInterPageSpacingKey来实现。


17

使用Swift,您可以使用initWithTransitionStyle:navigationOrientation:options:初始化器并将一个值传递给您的选项参数中的UIPageViewControllerOptionInterPageSpacingKey来设置页面之间的间距。

例如,以下代码显示了如何在Swift 2.2中实现它:

let optionsDict = [UIPageViewControllerOptionInterPageSpacingKey: 20]

let pageViewController = UIPageViewController(
    transitionStyle: .Scroll,
    navigationOrientation: .Horizontal,
    options: optionsDict
)

请注意,UIPageViewController类的参考资料中关于UIPageViewControllerOptionInterPageSpacingKey的说明:

该值应该是一个包装在NSNumber实例中的CGFloat。

然而,如前面的代码所示,类型为[String: Int]的字典也可以工作。


UIPageViewControllerOptionInterPageSpacingKey has changed to UIPageViewController.OptionsKey.interPageSpacing - Peterses

11

故事板(Storyboard)中选择UIPageViewController。 在属性检查器(Attributes Inspector)中,有一个设置页面间距(Page Spacing)的选项。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接