Swift 3 --Storyboard - UIPageViewController 实现导航控制器

1 创建一个空项目

拖一个UINavigationController 并且设置一个root controller


Swift 3 --Storyboard - UIPageViewController 实现导航控制器_第1张图片
1

2 在控件库里搜索 container view 控件, 拖入 root controller 空白处,删除 container view 自带的controller , 并设置layout填满controller

Swift 3 --Storyboard - UIPageViewController 实现导航控制器_第2张图片
2

3 在控件库搜索 pageViewcontroller 并添加 选中Container View,按住control键,用鼠标左键向第一个Page View Controller拖,放开左键,从弹出菜单中选择Embed

设置pageView 滚动方式

Swift 3 --Storyboard - UIPageViewController 实现导航控制器_第3张图片
0
Swift 3 --Storyboard - UIPageViewController 实现导航控制器_第4张图片
3

4接下来创建pagController中的子视图 创建三个controller 并创建类与之关联 我这里创建OneController,TwoController,ThreeController

Swift 3 --Storyboard - UIPageViewController 实现导航控制器_第5张图片
4

截止到现在我们的布局已经完成
接下来就是代码部分

class MainViewController: UIViewController {

var pageViewController: UIPageViewController!
var movieController: TwoController!
var musicController: OneController!
var bookController: ThreeController!
var controllers = [UIViewController]()

@IBOutlet var navView: UIView!



override func viewDidLoad() {
    super.viewDidLoad()

    self.navigationItem.titleView = self.navView
    
    //获取到嵌入的UIPageViewController
    pageViewController = self.childViewControllers.first as! UIPageViewController
    
    //根据Storyboard ID来创建一个View Controller
    movieController = storyboard?.instantiateViewController(withIdentifier: "MovieControllerID") as! TwoController
    musicController = storyboard?.instantiateViewController(withIdentifier: "MusicControllerID") as! OneController
    bookController = storyboard?.instantiateViewController(withIdentifier: "BookControllerID") as! ThreeController
    
    //设置pageViewController的数据源代理为当前Controller
    pageViewController.dataSource = self
    pageViewController.delegate = self
    
    //手动为pageViewController提供提一个页面
    pageViewController.setViewControllers([movieController], direction: UIPageViewControllerNavigationDirection.forward, animated: true, completion: nil)
    //把页面添加到数组中
    controllers.append(movieController)
    controllers.append(musicController)
    controllers.append(bookController)
    


}

@IBAction func OnePageAction(_ sender: Any) {
    
    pageViewController.setViewControllers([musicController], direction: UIPageViewControllerNavigationDirection.forward, animated: false, completion: nil)
    
}

@IBAction func twoViewController(_ sender: Any) {
    pageViewController.setViewControllers([movieController], direction: UIPageViewControllerNavigationDirection.forward, animated: false, completion: nil)
}

@IBAction func threePageAction(_ sender: Any) {
    pageViewController.setViewControllers([bookController], direction: UIPageViewControllerNavigationDirection.forward, animated: false, completion: nil)
}


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

}

extension MainViewController:UIPageViewControllerDataSource,UIPageViewControllerDelegate {

    //返回当前页面的下一个页面
    func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
        
        if viewController.isKind(of: TwoController.self) {
            return bookController
        }
        else if viewController.isKind(of: OneController.self) {
            return movieController
        }
        return nil
        
    }
    
    //返回当前页面的上一个页面
    func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
        
        if viewController.isKind(of: TwoController r.self) {
            return musicController
        }
        else if viewController.isKind(of: ThreeController.self) {
            return movieController
        }
        return nil
    }

func pageViewController(_ pageViewController: UIPageViewController, willTransitionTo pendingViewControllers: [UIViewController]) {
    
    
    
}

func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
    
}
}

你可能感兴趣的:(Swift 3 --Storyboard - UIPageViewController 实现导航控制器)