Search code examples
iosswiftuipageviewcontroller

Type 'UIPageViewController.OptionsKey' (aka 'NSString') has no member 'interPageSpacing'


I don't know what's the matter with Xcode. I want to create a UIPageViewController using the initializer in one of my company project:

init(transitionStyle style: UIPageViewController.TransitionStyle,
     navigationOrientation: UIPageViewController.NavigationOrientation, 
     options: [UIPageViewController.OptionsKey : Any]? = nil)

But Xcode keeps giving this error message.

This is my code:

let vc = UIPageViewController(transitionStyle: .scroll, 
                              navigationOrientation: .vertical, 
                              options: [UIPageViewController.OptionsKey.interPageSpacing: 10])

Meanwhile, in new demo project I created just now from the template Page-based App (as bellow), it builds without any error.

template

How to fix/walkaround it?


Solution

  • The syntax was different in Swift 4. Your current project might be in Swift 4.0. Change its swift version to 4.2 and above. Or use the below code

    init(transitionStyle style: UIPageViewControllerTransitionStyle, 
        navigationOrientation: UIPageViewControllerNavigationOrientation, 
        options: [String : Any]? = nil) 
    

    Swift 4.0

    let vc = UIPageViewController(transitionStyle: .scroll,
                                          navigationOrientation: .vertical,
                                          options: [UIPageViewControllerOptionInterPageSpacingKey : 10])
    

    Swift 4.2 and above

    let vc = UIPageViewController(transitionStyle: .scroll, 
                                  navigationOrientation: .vertical, 
                                  options: [UIPageViewController.OptionsKey.interPageSpacing : 10])