Search code examples
iosiphoneinitializationswift4uipageviewcontroller

swift ios Set transition style and orientation programmatically of PageViewController


I have View Controller and its super class is UIPageViewController I want to its transition style and orientation.

var navigationOrientation: UIPageViewControllerNavigationOrientation { 
get }
var transitionStyle: UIPageViewControllerTransitionStyle { get }

The value of this property is set when the page view controller is initialized, and cannot be changed. where exactly page is initialized in view controller and how to set these properties.


Solution

  • from where we call this page view controller for example on a button click method you can initialize just like this

    let vc = YourPageViewController(transitionStyle: 
    UIPageViewControllerTransitionStyle.scroll, navigationOrientation: 
    UIPageViewControllerNavigationOrientation.horizontal, options: nil)
    self.present(vc, animated: true, completion: nil)
    

    and in YourPageViewController you can implement initializer

    override init(transitionStyle style: UIPageViewControllerTransitionStyle, navigationOrientation: UIPageViewControllerNavigationOrientation, options: [String : Any]? = nil) {
            super.init(transitionStyle: style, navigationOrientation: navigationOrientation, options: nil)
        }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    

    the values passed from button click action will be set and you will have horizontal orientation and scroll transition style