UIPageViewController in Container not displaying other View Controllers - ios

Ok, could someone please help with my Swift code that I'm trying to fix.
I have read other question on here similar to mine but to no avail unfortunately.
The problem is that I have a UIPageViewController in a Container but when I test it on the iPhone, it won't display the two views I have set (there is actually 9 views but only put 2 in the code for now so I can get it working).
Here is my code:
import UIKit
class pvController: UIPageViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate {
var pages = [UIViewController]()
override func viewDidLoad() {
super.viewDidLoad()
self.delegate = self
self.dataSource = self
let page1: UIViewController! = storyboard?.instantiateViewController(withIdentifier: "vController1")
let page2: UIViewController! = storyboard?.instantiateViewController(withIdentifier: "vController2")
pages.append(page1)
pages.append(page2)
setViewControllers([page1], direction: UIPageViewControllerNavigationDirection.forward, animated: false)
// Do any additional setup after loading the view, typically from a nib.
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
let currentIndex = pages.index(of: viewController)
let previousIndex = abs((currentIndex! - 1) % pages.count)
return pages[previousIndex]
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
let currentIndex = pages.index(of: viewController)
let nextIndex = abs((currentIndex! + 1) % pages.count)
return pages[nextIndex]
}
func presentationCount(for pageViewController: UIPageViewController) -> Int {
return pages.count
}
func presentationIndex(for pageViewController: UIPageViewController) -> Int {
return 0
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
I did initially try doing it with SwipeGestureRecogniser although I couldn't get it to work/figure it out.
Also I am fairly new to this so it may be something simple that I just can't figure out.
Thanks in advance

Related

Adjust containerView Height based on PageviewController

I'm kinda having a trouble adjusting the ContainerView height based on the height of the pageViewController.
So on this example I have two pages : "Yellow Page","Purple Page"
Yellow page = 120px Height
Purple page = 250px Height
All I wanna work out is to adjust the height automatically as I scroll on the pageViewController embedded on the containerView.
Hope you can help, thank you!
Here's the Image.
Storyboard Image
This is my PageViewController Code.
import UIKit
class PageViewController: UIPageViewController,UIPageViewControllerDataSource, UIPageViewControllerDelegate {
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
let currentIndex = pages.index(of: viewController)!
let previousIndex = abs((currentIndex - 1) % pages.count)
return pages[previousIndex]
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
let currentIndex = pages.index(of: viewController)!
let nextIndex = abs((currentIndex + 1) % pages.count)
return pages[nextIndex]
}
var pages = [UIViewController]()
override func viewDidLoad() {
super.viewDidLoad()
self.delegate = self
self.dataSource = self
let page1: UIViewController! = storyboard?.instantiateViewController(withIdentifier: "page1");
let page2: UIViewController! = storyboard?.instantiateViewController(withIdentifier: "page2");
pages.append(page1)
pages.append(page2)
setViewControllers([page1], direction: UIPageViewControllerNavigationDirection.forward, animated: false, completion: nil)
}
func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int {
return pages.count
}
func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int {
return 0
}
override func viewDidLayoutSubviews() {
}
}
You can hook the height of the container and controls it from the pager like this
#IBOutlet weak var conH: NSLayoutConstraint!
//
func callMe(value:CGFloat) {
conH.constant = value
self.view.layoutIfNeeded()
}
//
func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
let vc = UIApplication.shared.keyWindow?.rootViewController as! ViewController
let currentIndex = pages.index(of: previousViewControllers.first!)!
let nextIndex = abs((currentIndex + 1) % pages.count)
vc.callMe(value:nextIndex == 0 ? 100 : 200)
}
//

no page control displayed even after implementing delegate methods

Page view controller not display after adding delegate and datasource. how to implement pageViewController Datasource and delegate
import UIKit
DataSource
class PageViewController: UIPageViewController ,UIPageViewControllerDataSource,UIPageViewControllerDelegate
{
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
let vc = storyboard?.instantiateViewController(withIdentifier: "vc") as! ViewController
return vc
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
let vc = storyboard?.instantiateViewController(withIdentifier: "vc") as! ViewController
return vc
}
func presentationCount(for pageViewController: UIPageViewController) -> Int {
return 3
}
override func index(ofAccessibilityElement element: Any) -> Int {
return 0
}
setting a delegate and datasource in ViewDidLoad
override func viewDidLoad() {
super.viewDidLoad()
dataSource=self
delegate = self
let vc = storyboard?.instantiateViewController(withIdentifier: "vc") as! ViewController
self.setViewControllers([vc], direction: .forward, animated: true, completion: nil)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Replacing
override func index(ofAccessibilityElement element: Any) -> Int {
return 0
}
with
func presentationIndex(for pageViewController: UIPageViewController) -> Int {
return 0
}
did the work.
I was calling wrong method

transition page with uipageviewcontroller

I have a view container containing a UIPageViewController. to him they are associated with 2 viewController of UIImageView. It works almost everything. I can not change the animations to scroll through images. the animation is always that of the page curl while I would like a simple scroll. how to do?
class MyPageViewController: UIPageViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate {
var pages = [UIViewController]()
override func viewDidLoad() {
super.viewDidLoad()
self.delegate = self
self.dataSource = self
let page1: UIViewController! = storyboard?.instantiateViewController(withIdentifier: "page1")
let page2: UIViewController! = storyboard?.instantiateViewController(withIdentifier: "page2")
pages.append(page1)
pages.append(page2)
setViewControllers([page1], direction: UIPageViewControllerNavigationDirection.forward, animated: false, completion: nil)
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
let currentIndex = pages.index(of: viewController)!
let previousIndex = abs((currentIndex - 1) % pages.count)
return pages[previousIndex]
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
let currentIndex = pages.index(of: viewController)!
let nextIndex = abs((currentIndex + 1) % pages.count)
return pages[nextIndex]
}
private func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int {
return pages.count
}
private func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int {
return 0
}
}
How are you initiating the UIPageViewController?
From code, you can specify a transitionStyle in the constructor, as seen here
From storyboard, you can specify the same transitionStyle in the storyboard itself, as shown in this SO answer

UIPageViewContoller in container view with page control icon

I am writing a Swift app with three views. I have achieved this by putting a UIPageViewController inside a container and having that switch when you swipe, from another Stack Overflow article: UIPageViewController and storyboard. I would also like to have dots on the bottom indicating the page you are on. But, the UIPageControl I am using can only be placed on the view controller with the container, and the code with the current page is in the page controller's code. Here is my code:
class ContainerView : UIViewController {
#IBOutlet var myPageController: UIPageControl!
}
class MyPageViewController: UIPageViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate {
var pages = [UIViewController]()
override func viewDidLoad() {
super.viewDidLoad()
self.delegate = self
self.dataSource = self
let page1: UIViewController! = storyboard?.instantiateViewController(withIdentifier: "Page1ID")
let page2: UIViewController! = storyboard?.instantiateViewController(withIdentifier: "Page2ID")
let page3: UIViewController! = storyboard?.instantiateViewController(withIdentifier: "Page3ID")
pages.append(page1)
pages.append(page2)
pages.append(page3)
setViewControllers([page1], direction: UIPageViewControllerNavigationDirection.forward, animated: false, completion: nil)
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
let currentIndex = pages.index(of: viewController)!
if (currentIndex == 0) {
return pages[currentIndex]
}
let previousIndex = abs((currentIndex - 1) % pages.count)
//myPageControl.currentPage = previousIndex
return pages[previousIndex]
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
let currentIndex = pages.index(of: viewController)!
if (currentIndex == 2) {
return pages[currentIndex]
}
let nextIndex = abs((currentIndex + 1) % pages.count)
//myPageControl.currentPage = nextIndex
return pages[nextIndex]
}
func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int {
return pages.count
}
func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int {
return 0
}
}
How could I access the parent view controller (ContainerView) from the child in the container (MyPageViewController)? I'm new to Swift/Objective-C but have programmed in other languages (C++, Python). If you need the storyboard, please ask.

UIPageControl to show current page

I made a PageViewController to switch between different View Controllers, and i'd like to show which page we see. I added a pageControl, and gave it the following code.
pageControl.currentPageIndicatorTintColor = UIColor.blueColor()
pageControl.pageIndicatorTintColor = UIColor.lightGrayColor()
pageControl.numberOfPages = pages.count
The complete code:
import UIKit
class ViewControllerSportinfrastructuur: UIViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate {
#IBOutlet weak var pageControl: UIPageControl!
var pageViewController: UIPageViewController!
let pages = ["PageOneViewController", "PageTwoViewController"]
//MARK: - page view controller data source
func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {
if let index = pages.indexOf(viewController.restorationIdentifier!) {
if index > 0 {
return viewControllerAtIndex(index - 1)
}
}
return nil
}
func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {
if let index = pages.indexOf(viewController.restorationIdentifier!) {
if index < pages.count - 1 {
return viewControllerAtIndex(index + 1)
}
}
return nil
}
func viewControllerAtIndex(index: Int) -> UIViewController? {
let vc = storyboard?.instantiateViewControllerWithIdentifier(pages[index])
return vc
}
override func viewDidLoad() {
super.viewDidLoad()
if let vc = storyboard?.instantiateViewControllerWithIdentifier("MyPageViewController") {
self.addChildViewController(vc)
self.view.addSubview(vc.view)
pageViewController = vc as! UIPageViewController
pageViewController.dataSource = self
pageViewController.delegate = self
pageViewController.setViewControllers([viewControllerAtIndex(0)!], direction: .Forward, animated: true, completion: nil)
pageViewController.didMoveToParentViewController(self)
pageControl.currentPageIndicatorTintColor = UIColor.blueColor()
pageControl.pageIndicatorTintColor = UIColor.lightGrayColor()
pageControl.numberOfPages = pages.count
}
}
Does anyone know what code i should use to set up the current page? Thanks in advance!
EDIT: found the answer already!
I added pageControl.currentPage = index in following functions:
func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {
if let index = pages.indexOf(viewController.restorationIdentifier!) {
pageControl.currentPage = index
if index > 0 {
return viewControllerAtIndex(index - 1)
}
}
return nil
}
And did the same for the viewControllerAfterViewController func!
The UIPageViewController provides a default UIPageControl - I would recommend to make use of it if you don't have particular custom requirements.
In order to show it you need to implement the following two optional methods in your UIPageViewControllerDataSource:
func presentationCount(for pageViewController: UIPageViewController) -> Int {
return n //number of view controllers
}
func presentationIndex(for pageViewController: UIPageViewController) -> Int {
return i //index of the visible view controller
}
Set pageViewController.delegate = self, implement protocol UIPageViewControllerDelegate
and use on of these methods to set self.pageControl.currentPage = ...
optional func pageViewController(_ pageViewController: UIPageViewController,
willTransitionToViewControllers pendingViewControllers: [UIViewController])
optional func pageViewController(_ pageViewController: UIPageViewController,
didFinishAnimating finished: Bool,
previousViewControllers previousViewControllers: [UIViewController],
transitionCompleted completed: Bool)

Resources