Tunko Development Diary

[iOS][swift] PageviewController 사용하기 본문

Development/iOS 개발

[iOS][swift] PageviewController 사용하기

Tunko 2020. 1. 2. 01:20

UIPageViewController 사용

각 뷰에 컨테이너로 들어갈 스토리보드 아이디를 매칭한다.


import UIKit 
class HomeViewController: UIViewController, UIPageViewControllerDataSource 
{
    var pageViewController : UIPageViewController! 
    var pageViewName : NSArray!         // 컨텐츠 뷰 컨트롤러 이름 
    var pageStoryBoardID : NSArray! 

    override func viewDidLoad() 
    {
        super.viewDidLoad() 

        let i = NetworkManager.shared.netWorkTest() 
        self.pageViewName = NSArray(objects: "HomeContentViewController", "RankContentViewController", "ReviewContentViewController", "ShoppingContentViewController", "MeasurementContentViewController") 

        self.pageStoryBoardID = NSArray(objects: "HomePageViewController", "RankPageViewController", "ReviewPageViewController", "ShoppingPageViewController", "MeasurementPageViewController") 

        self.pageViewController = self.storyboard?.instantiateViewController(withIdentifier: "HomePageViewController") as? UIPageViewController 

        self.pageViewController.dataSource = self as UIPageViewControllerDataSource 

        let startVC = self.viewControllerAtIndex(index: 0) as RootPageViewController 
        let viewControllers = NSArray(object: startVC) 

        self.pageViewController.setViewControllers(viewControllers as? [UIViewController], direction: .forward, animated: true, completion: nil) 

        let width : CGFloat = CGFloat(self.view.frame.width) 
        let height : CGFloat = CGFloat(self.view.frame.height - 100)  

        self.addChild(self.pageViewController) 
        self.view.addSubview(self.pageViewController.view) 
    }

    func viewControllerAtIndex(index : Int) -> RootPageViewController 
    {
        let contentName : String = pageViewName[index] as! String 

            let vc : RootPageViewController = self.storyboard?.instantiateViewController(withIdentifier: contentName) as! RootPageViewController 

        vc.pageIndex = index 

        return vc 
    }

    func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? 
    {
        let vc = viewController as! RootPageViewController 
        var index = vc.pageIndex as Int 

        if(index == 0 || index == NSNotFound) 
        {
            return nil 
        }

        index -= 1 

        return self.viewControllerAtIndex(index: index) 
    }

    func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? 
    {
        let vc = viewController as! RootPageViewController 
        var index = vc.pageIndex as Int 

        if (index == NSNotFound) 
        {
            return nil 
        }

        index += 1 

        if(index == self.pageViewName.count) { 
            return nil 
        }

        return self.viewControllerAtIndex(index: index) 
    }

    func presentationCount(for pageViewController: UIPageViewController) -> Int { 
        return self.pageStoryBoardID.count 
    }

    func presentationIndex(for pageViewController: UIPageViewController) -> Int { 
        return 0 
    }
}
반응형
Comments