Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Horizontal padding between UIPageViewController child view controllers

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

like image 885
denvdancsk Avatar asked Dec 20 '13 01:12

denvdancsk


3 Answers

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.

like image 134
Imanou Petit Avatar answered Nov 07 '22 21:11

Imanou Petit


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

like image 42
Sarath Neeravallil Avatar answered Nov 07 '22 23:11

Sarath Neeravallil


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

like image 18
tassar Avatar answered Nov 07 '22 23:11

tassar