13

I am using UIPageViewController to display images that are embedded in child view controllers.

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];

Everything works great but I hate that the child view controllers are right next to one another.

enter image description here

I was wondering if there was a way to add padding between the child view controllers so that way they're not right next to one another.

Something that would look more like this:

enter image description here

denvdancsk
  • 3,033
  • 2
  • 26
  • 41

3 Answers3

18

check UIPageViewController init method

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

you can pass your inter page space value to opt in UIPageViewControllerOptionInterPageSpacingKey

tassar
  • 578
  • 3
  • 9
17

With Swift, you can use initWithTransitionStyle:navigationOrientation:options: initializer and pass a value to UIPageViewControllerOptionInterPageSpacingKey in your options parameter to set a space between pages.

As an example, the following code shows how to implement it with Swift 2.2:

let optionsDict = [UIPageViewControllerOptionInterPageSpacingKey: 20]

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

Note that the UIPageViewController class reference states about UIPageViewControllerOptionInterPageSpacingKey:

The value should be a CGFloat wrapped in an instance of NSNumber.

However, a Dictionary of type [String: Int], as shown in the previous code, also works.

Imanou Petit
  • 89,880
  • 29
  • 256
  • 218
  • `UIPageViewControllerOptionInterPageSpacingKey` has changed to `UIPageViewController.OptionsKey.interPageSpacing` – Peterses Aug 07 '21 at 00:35
11

Select the UIPageViewController from the Storyboard. Inside the Attributes Inspector there is an option for setting the Page Spacing.