简体   繁体   中英

page view controller not transitioning in the style it is coded to

Hello new to swift/iphone development, any advice is appreciated!

Making a simple page based app consisting of 3 pages, however the view controllers transition using the .pageTurn style when the transition style is set to .scroll in my code

let pageController = ViewController(transitionStyle: .scroll, navigationOrientation: .horizontal, options: nil)

Quite confused why the transition style does not follow what i have coded?

full code:

 let pageController = ViewController(transitionStyle: .scroll, navigationOrientation: .horizontal, options: nil)

 class ViewController: UIPageViewController {

// page view controllers here
let cardsVC: UIViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "CardsNavController")
let profileVC: UIViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ProfileNavController")
let matchesVC: UIViewController! = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "MatchesNavController")

override func viewDidLoad() {
    super.viewDidLoad()

    view.backgroundColor = UIColor.white
    dataSource = self
    // loads first view
    setViewControllers([profileVC], direction: .forward, animated: true, completion: nil)
}


// load left view
func goToNextVC() {
    let nextVC = pageViewController(self, viewControllerAfter: viewControllers![0] )!
    setViewControllers([nextVC], direction: .forward, animated: true, completion: nil)
}
// load right view
func goToPreviousVC() {
    let previousVC = pageViewController(self, viewControllerBefore: viewControllers![0] )!
    setViewControllers([previousVC], direction: .reverse, animated: true, completion: nil)
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
     }


 }

 // MARK - UIPageViewControllerDataSource
 extension ViewController: UIPageViewControllerDataSource {

func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {

    // navigate right
    switch viewController {
    case cardsVC:
        return profileVC
    case profileVC:
        return nil
    case matchesVC:
        return cardsVC
    default:
        return nil
    }
}

// navigate left
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {

    switch viewController {
    case cardsVC:
        return matchesVC
    case profileVC:
        return cardsVC
    case matchesVC:
        return nil
    default:
        return nil
    }
}

}

Use like this :

required init?(coder: NSCoder) {
    super.init(transitionStyle: .scroll, navigationOrientation: .horizontal, options: nil)
}

Have a look at following Project it will Give you expected result I had used here a containerView To present ViewControllers with tableViews using pageViewController

Link - https://drive.google.com/open?id=0B2WiGVeE-REPT3loNUFPa0llWGs

ScreenShot of StoryBoard

在此处输入图片说明

Output - 在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM