Matt Spoon
Matt Spoon

Reputation: 2830

Adding a UIPageViewController into an already existing view controller with a frame smaller than the entire view controller

I have a container view in one view controller (FirstViewController) which embeds a page view controller. The purpose of having it inside of the container view is to limit the bounds of the page view. Here is my code:

private var pagesViewController : UIPageViewController?
override func viewDidLoad() {
    let pageController = self.storyboard!.instantiateViewControllerWithIdentifier("PageController") as! UIPageViewController
    pageController.dataSource = self

    let firstController = getItemController(0)!
    let startController: NSArray = [firstController]
    pageController.setViewControllers(startController as! [UIViewController], direction: UIPageViewControllerNavigationDirection.Forward, animated: false, completion: nil)

    pagesViewController = pageController
    addChildViewController(pagesViewController!)
    self.view.addSubview(pagesViewController!.view)
    pagesViewController!.didMoveToParentViewController(self)

    let appearance = UIPageControl.appearance()
    appearance.pageIndicatorTintColor = UIColor.grayColor()
    appearance.currentPageIndicatorTintColor = UIColor.whiteColor()
    appearance.backgroundColor = UIColor.darkGrayColor()
}

This code adds the page view to the superview

But when I run the app, the page view takes up the entire screen. I would like it to be in the bounds of the container view.

Upvotes: 1

Views: 818

Answers (1)

Matt Spoon
Matt Spoon

Reputation: 2830

nvm, I figured it out. You can put a regular view controller onto the storyboard, then embed that in the container view instead Then you implement the code to add the page view controller to the view which you just embedded. Hope this might help somebody in future :)

Upvotes: 1

Related Questions