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.
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:
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.
Select the UIPageViewController from the Storyboard. Inside the Attributes Inspector there is an option for setting the Page Spacing.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With