页面视图控制器未按其编码的样式进行转换

时间:2017-10-10 21:51:38

标签: ios iphone swift xcode

您好swift / iphone开发新手,欢迎任何建议!

创建一个包含3个页面的基于页面的简单应用程序,但是当我的代码中将过渡样式设置为.scroll时,视图控制器使用.pageTurn样式进行转换

  

让pageController = ViewController(transitionStyle:.scroll,navigationOrientation:.horizo​​ntal,options:nil)

很难混淆为什么过渡风格不符合我的编码?

完整代码:

 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
    }
}

}

2 个答案:

答案 0 :(得分:0)

像这样使用:

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

答案 1 :(得分:0)

看看下面的项目它会给你预期的结果 我在这里使用了一个containerView使用pageViewController

向tableViews呈现ViewControllers
Link - https://drive.google.com/open?id=0B2WiGVeE-REPT3loNUFPa0llWGs

StoryBoard的ScreenShot

enter image description here

输出 - enter image description here