I have 3 types of VC's , 1st Vc, 2nd VC is PageViewController with 3 pages(I added PageController to normal ViewController here), third one is FinalViewController.
1st VC has button when i click it will move to 2nd VC that is PageViewController(It has 3 pages[3 ViewConrollers] working fine).
In PageViewController after loading first VC, it has view with gestures when i tap that i want to navigate FinalViewController completely.
Here navigation not working.
My 1st VC code
override func viewWillAppear(_ animated: Bool) {
self.navigationController?.navigationBar.isHidden = true
}
override func viewWillDisappear(_ animated: Bool) {
self.navigationController?.navigationBar.isHidden = false
}
#IBAction func btn(_ sender: Any) {
let storyboard = self.storyboard?.instantiateViewController(withIdentifier: "NVC")
self.navigationController?.pushViewController(storyboard!, animated: true)
}
My PageViewController code
import UIKit
class NewViewController: UIViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate {
#IBOutlet weak var pagerController: UIPageControl!
#IBOutlet weak var pageControllerView: UIView!
// The pages it contains
var pages = [UIViewController]()
// The UIPageViewController
var pageContainer: UIPageViewController!
// Track the current index
var currentIndex: Int?
private var pendingIndex: Int?
override func viewDidLoad() {
super.viewDidLoad()
// Setup the pages
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let page1 = storyboard.instantiateViewController(withIdentifier: "ViewController") as! ViewController
let page2: UIViewController! = storyboard.instantiateViewController(withIdentifier: "SecondViewController")
let page3: UIViewController! = storyboard.instantiateViewController(withIdentifier: "ThirdViewController")
pages.append(page1)
pages.append(page2)
pages.append(page3)
page1.variable = "This is strig..."
// Create the page container
pageContainer = UIPageViewController(transitionStyle: .scroll, navigationOrientation: .horizontal, options: nil)
pageContainer.delegate = self
pageContainer.dataSource = self
pageContainer.setViewControllers([page1], direction: UIPageViewController.NavigationDirection.forward, animated: false, completion: nil)
// Add it to the view
pageControllerView.addSubview(pageContainer.view)
// Configure our custom pageControl
view.bringSubviewToFront(pagerController)
pagerController.numberOfPages = pages.count
pagerController.currentPage = 0
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - UIPageViewController delegates
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
let currentIndex = pages.firstIndex(of:viewController)!
if currentIndex == 0 {
return nil
}
let previousIndex = abs((currentIndex - 1) % pages.count)
return pages[previousIndex]
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
let currentIndex = pages.firstIndex(of:viewController)!
if currentIndex == pages.count-1 {
return nil
}
let nextIndex = abs((currentIndex + 1) % pages.count)
return pages[nextIndex]
}
func pageViewController(_ pageViewController: UIPageViewController, willTransitionTo pendingViewControllers: [UIViewController]) {
pendingIndex = pages.firstIndex(of:pendingViewControllers.first!)
}
func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
if completed {
currentIndex = pendingIndex
if let index = currentIndex {
pagerController.currentPage = index
}
}
}
}
My `ViewConroller` code means which is loading in `PageViewController`
import UIKit
class ViewController: UIViewController {
var variable = String()
#IBOutlet weak var subView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
//Add gesture to incoming call view
let incomingCallsViewTap = UITapGestureRecognizer(target: self, action: #selector(incomingCallsViewTapFunction(_:)))
self.subView!.addGestureRecognizer(incomingCallsViewTap)
}
// Tap gestrure selector fuction
#objc func incomingCallsViewTapFunction(_ sender: UITapGestureRecognizer) {
let clvc = self.storyboard?.instantiateViewController(withIdentifier: "FVC") as! FinalViewController
self.navigationController?.pushViewController(clvc, animated: false)
}
#IBAction func btn(_ sender: Any) {
print("ViewController one")
print(variable)
}
}
navigationViewController is nil so I had to do the following:
(UIApplication.shared.keyWindow?.rootViewController as? UINavigationController)?.pushViewController(clvc, animated: true)
// Tap gestrure selector fuction
#objc func incomingCallsViewTapFunction(_ sender: UITapGestureRecognizer) {
let clvc = self.storyboard?.instantiateViewController(withIdentifier: "FVC") as! FinalViewController
// self.navigationController?.pushViewController(clvc, animated: false)
(UIApplication.shared.keyWindow?.rootViewController as? UINavigationController)?.pushViewController(clvc, animated: true)
}
Related
I have three classes OnboardingViewController, OnboardingPageViewController, and OnboardingContentViewController.
In my ViewController class, I have a UIPageControl that updates whenever I press the UIButton "Next" beneath it. This is working as intended. However I want to make the UIPageControl updated according to when I swipe left/right on the UIPageViewController, but I'm not sure how to do this.
class OnboardingViewController: UIViewController {
#IBOutlet weak var nextButton: UIButton!
#IBOutlet weak var pageControl: UIPageControl!
var onboardingPageViewController: OnboardingPageViewController?
#IBAction func nextButtonTapped(_ sender: UIButton) {
if let index = onboardingPageViewController?.currentPageIndex {
switch index {
case 0:
onboardingPageViewController?.nextPage()
case 1:
onboardingPageViewController?.nextPage()
nextButton.setTitle("Get Started", for: .normal)
case 2:
//present login ViewController after final next button is tapped
default:
break
}
pageControl.currentPage = index + 1
}
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let destination = segue.destination
if let pageViewController = destination as? OnboardingPageViewController {
onboardingPageViewController = pageViewController
}
}
}
class OnboardingContentViewController: UIViewController {
#IBOutlet var imageView: UIImageView!
#IBOutlet var titleLabel: UILabel!
#IBOutlet var introLabel: UILabel!
var index = 0
var contentTitle = ""
var intro = ""
var imageFile = ""
override func viewDidLoad() {
super.viewDidLoad()
titleLabel.text = contentTitle
introLabel.text = intro
imageView.image = UIImage(named: imageFile)
}
}
class OnboardingPageViewController: UIPageViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate {
var pageTitle = ["Title1", "Title2", "Title3"]
var pageIntro = ["Description1","Description2","Description3"]
var pageImages = ["image1","image2","image3"]
var currentPageIndex = 0
override func viewDidLoad() {
super.viewDidLoad()
dataSource = self
delegate = self
if let startingViewController = contentViewController(at: currentPageIndex) {
setViewControllers([startingViewController], direction: .forward,animated: true, completion: nil)
}
}
func nextPage() {
currentPageIndex += 1
if let nextViewController = contentViewController(at: currentPageIndex){
setViewControllers([nextViewController], direction:.forward, animated: true, completion:nil)
}
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
var index = (viewController as! OnboardingContentViewController).index
index -= 1
return contentViewController(at:index)
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
var index = (viewController as! OnboardingContentViewController).index
index += 1
return contentViewController(at:index)
}
func contentViewController(at index: Int) -> OnboardingContentViewController? {
if index < 0 || index >= pageIntro.count {
return nil
}
let storyboard = UIStoryboard(name: "Onboarding", bundle: nil)
if let pageContentViewController = storyboard.instantiateViewController(withIdentifier:"OnboardingContentViewController") as? OnboardingContentViewController {
pageContentViewController.imageFile = pageImages[index]
pageContentViewController.contentTitle = pageTitle[index]
pageContentViewController.intro = pageIntro[index]
pageContentViewController.index = index
return pageContentViewController
}
return nil
}
}
I tried adding the UIPageViewController delegate method to the first view controller but I'm finding that nothing is printing in the console. What am I missing?
class OnboardingViewController: UIViewController, UIPageViewControllerDelegate {
#IBOutlet weak var pageControl: UIPageControl!
override func viewDidLoad() {
super.viewDidLoad()
let pageController = self.storyboard!.instantiateViewController(withIdentifier: "OnboardingPageViewController") as! UIPageViewController
pageController.delegate = self
}
func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
if completed == true {
print("change page control number")
}
}
When you do let pageController = self.storyboard!.instantiateViewController..., you're initializing a new copy of the page view controller. This is unrelated to the one that your users see.
/// no!
let pageController = self.storyboard!.instantiateViewController(withIdentifier: "OnboardingPageViewController") as! UIPageViewController
pageController.delegate = self
Instead, you need to reference the existing page view controller that was added in the storyboard. It's automatically added as the first child of OnboardingViewController, so you can get it using self.children[0].
class OnboardingViewController: UIViewController, UIPageViewControllerDelegate {
#IBOutlet weak var pageControl: UIPageControl!
/// here!
var pageController: UIPageViewController {
return self.children[0] as! UIPageViewController
}
override func viewDidLoad() {
super.viewDidLoad()
pageController.delegate = self
}
func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
if completed == true {
print("change page control number")
}
}
}
Easiest way to create a post notification Here
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
var index = (viewController as! OnboardingContentViewController).index
index -= 1
// post notification here with sending index value
return contentViewController(at:index)
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
var index = (viewController as! OnboardingContentViewController).index
index += 1
// post notification here with sending index value
return contentViewController(at:index)
}
And listen in viewDidLoad of OnboardingViewController
I have PageViewController and I'm loading ViewController's, every thing working fine. But when i update data in my ViewController of PageViewController it's getting crash in SecondViewController lbl1.text = "New Lbl1".
Error: Fatal error: Unexpectedly found nil while implicitly unwrapping
an Optional value
My Code in PageViewController :
import UIKit
class NewViewController: UIViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate {
#IBOutlet weak var pagerController: UIPageControl!
#IBOutlet weak var pageControllerView: UIView!
// The pages it contains
var pages = [UIViewController]()
// The UIPageViewController
var pageContainer: UIPageViewController!
// Track the current index
var currentIndex: Int?
private var pendingIndex: Int?
override func viewDidLoad() {
super.viewDidLoad()
// Setup the pages
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let page1 = storyboard.instantiateViewController(withIdentifier: "ViewController") as! ViewController
let page2: UIViewController! = storyboard.instantiateViewController(withIdentifier: "SecondViewController")
let page3: UIViewController! = storyboard.instantiateViewController(withIdentifier: "ThirdViewController")
pages.append(page1)
pages.append(page2)
pages.append(page3)
page1.variable = "This is strig..."
// Create the page container
pageContainer = UIPageViewController(transitionStyle: .scroll, navigationOrientation: .horizontal, options: nil)
pageContainer.delegate = self
pageContainer.dataSource = self
pageContainer.setViewControllers([page1], direction: UIPageViewController.NavigationDirection.forward, animated: false, completion: nil)
// Add it to the view
pageControllerView.addSubview(pageContainer.view)
// set the frame of the pageContainer view to match its superview (the red view)
pageContainer.view.frame = pageControllerView.bounds
// let it resize if needed (such as device rotation)
pageContainer.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
// Configure our custom pageControl
view.bringSubviewToFront(pagerController)
pagerController.numberOfPages = pages.count
pagerController.currentPage = 0
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - UIPageViewController delegates
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
let currentIndex = pages.firstIndex(of:viewController)!
if currentIndex == 0 {
return nil
}
let previousIndex = abs((currentIndex - 1) % pages.count)
return pages[previousIndex]
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
let currentIndex = pages.firstIndex(of:viewController)!
if currentIndex == pages.count-1 {
return nil
}
let nextIndex = abs((currentIndex + 1) % pages.count)
return pages[nextIndex]
}
func pageViewController(_ pageViewController: UIPageViewController, willTransitionTo pendingViewControllers: [UIViewController]) {
pendingIndex = pages.firstIndex(of:pendingViewControllers.first!)
}
func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
if completed {
currentIndex = pendingIndex
if let index = currentIndex {
pagerController.currentPage = index
}
}
}
#IBAction func barBtnAction(_ sender: UIBarButtonItem) {
SecondViewController().setNewData()
}
}
My code in ViewController :
import UIKit
class SecondViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
#IBOutlet weak var tblView: UITableView!
#IBOutlet weak var tblHeight: NSLayoutConstraint!
#IBOutlet weak var lbl1: UILabel!
#IBOutlet weak var lbl2: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
tblView.delegate = self
tblView.dataSource = self
}
#IBAction func btn(_ sender: Any) {
print("ViewController second")
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell")
cell?.textLabel?.text = "\(indexPath.row)"
return cell!
}
func setNewData() {
lbl1.text = "New Lbl1"
lbl2.text = "New Lbl2"
tblHeight.constant = 250
}
}
My PageVC
My SecondVC
This
SecondViewController().setNewData()
access the 2 nd vc without layout as you should load it from storyboard regradless of outlet connected / not hence a crash , you may need
let sec = pages[1] as! SecondViewController
sec.setNewData()
This SecondViewController() is used when the vc is completely created programmatically when you do it with 1 from storyboard all outlets will be nil as layout isn't loaded with that initializer in addition to that you create a new instance other than the 1 in pager controller
This question already has answers here:
Refresh UIPageViewController - reorder pages and add new pages
(4 answers)
Closed 4 years ago.
I am looking to how to reload the UI page controller.
On a table VC I have added UIHeaderView and embedded the UIPage VC.
Page VC is loading images dynamically from firebase.
First time it is loading the right set of images but when on the table VC I selecting other cel the Page VC need to refresh its data.
How to achieve this functionality
Here is the coed I have written for PAGE VC
//-----PAGE VC CODE------
import UIKit
import Firebase
protocol ProductImagesPageVCDelegate: class
{
func setupPageController(numberOfPages: Int)
func turnPageController(to index: Int)
}
class ProductImagesPageVC: UIPageViewController {
var product: Product!
weak var pageViewControllerDelegate: ProductImagesPageVCDelegate?
struct StoryBoard {
static let productImageVC = "ProductImageVC"
}
lazy var controllers: [UIViewController] = {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
var controllers = [UIViewController]()
if let imageLinks = self.product.imageLinks
{
for imageLink in imageLinks
{
let productImageVC = storyboard.instantiateViewController(withIdentifier: StoryBoard.productImageVC)
controllers.append(productImageVC)
}
}
self.pageViewControllerDelegate?.setupPageController(numberOfPages: controllers.count)
return controllers
}()
override func viewDidLoad() {
super.viewDidLoad()
// if #available(iOS 11.0, *) {
// contentInsetAdjustmentBehavior = .never
// } else {
// automaticallyAdjustsScrollViewInsets = false
// }
automaticallyAdjustsScrollViewInsets = false
dataSource = self
delegate = self
self.turnToPage(index: 0)
}
func turnToPage(index: Int)
{
let controller = controllers[index]
var direction = UIPageViewControllerNavigationDirection.forward
if let currentVC = viewControllers?.first
{
guard let currentIndex = controllers.index(of: currentVC) else {return}
if currentIndex > index
{
direction = .reverse
}
}
self.configuewDisplaying(viewController: controller)
setViewControllers([controller], direction: direction, animated: true, completion: nil)
}
func configuewDisplaying(viewController: UIViewController)
{
for (index, vc) in controllers.enumerated()
{
if viewController === vc {
if let productImageVC = viewController as? ProductImageVC
{
productImageVC.imageLink = self.product.imageLinks?[index]
self.pageViewControllerDelegate?.turnPageController(to: index)
}
}
}
}
}
extension ProductImagesPageVC: UIPageViewControllerDataSource
{
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
if let index = controllers.index(of: viewController)
{
if index < controllers.count - 1
{
return controllers[index + 1]
}
}
return controllers.first
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
if let index = controllers.index(of: viewController)
{
if index > 0
{
return controllers[index - 1]
}
}
return controllers.last
}
}
extension ProductImagesPageVC: UIPageViewControllerDelegate
{
func pageViewController(_ pageViewController: UIPageViewController, willTransitionTo pendingViewControllers: [UIViewController]) {
self.configuewDisplaying(viewController: pendingViewControllers.first as! ProductImageVC)
}
func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
if !completed
{
self.configuewDisplaying(viewController: previousViewControllers.first as! ProductImageVC)
}
}
}
Try this-
let viewControllers: [UIViewControllers] = [UIViewController()]
if let pageViewController = parentViewController as? UIPageViewController {
pageViewController.setViewControllers(viewControllers, direction: .Forward, animated: true, completion: nil)}
I have the class SliderPgaeViewController: UIPageViewController with scroll transition style as follows:
class SliderPgaeViewController: UIPageViewController, UIPageViewControllerDelegate, UIPageViewControllerDataSource, PlayerUpdatePageControllerDelegate {
var lastPendingIndex: Int = 0
var sliderPageDelegate: SliderPageDelegate? = nil
let playerManager = PlayerManager.getInstance()
override func viewDidLoad() {
super.viewDidLoad()
self.dataSource = self
self.delegate = self
setViewControllers([createViewController(index: playerManager.getCurrentIndex())!], direction: .forward, animated: true, completion: nil)
lastPendingIndex = playerManager.getCurrentIndex()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func presentationCount(for pageViewController: UIPageViewController) -> Int {
return playerManager.getSongsCount()
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
if let vc = viewController as? PlayerImageViewController {
if (vc.index == 0){
return nil
}
return createViewController(index: vc.index! - 1)
}
return nil
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
if let vc = viewController as? PlayerImageViewController {
if (vc.index == playerManager.getSongsCount() - 1){
return nil
}
return createViewController(index: vc.index! + 1)
}
return nil
}
func pageViewController(_ pageViewController: UIPageViewController, willTransitionTo pendingViewControllers: [UIViewController]){
if let vc = pendingViewControllers[0] as? PlayerImageViewController {
self.lastPendingIndex = vc.index!
}
}
func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool,previousViewControllers: [UIViewController],transitionCompleted completed: Bool) {
print("before completion : \(self.lastPendingIndex)")
if(completed){
print("completed : \(self.lastPendingIndex)")
if (viewControllers?.first as? PlayerImageViewController) != nil {
sliderPageDelegate?.updateSong(index: self.lastPendingIndex, dir: 0)
}
}
}
private func createViewController(index i: Int) -> UIViewController?{
let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "PlayerImageController") as! PlayerImageViewController
vc.index = i
vc.image = playerManager.getSong(index: i).image
return vc
}
...
I am using this page controller to display the thumbnail of a song in a music player. And when the user turn a page the player changes the song playing by calling sliderPageDelegate?.updateSong(index: self.lastPendingIndex, dir: 0) in pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool,previousViewControllers: [UIViewController],transitionCompleted completed: Bool)
When I'm turning the page forward and then immediately backward, the page is turning correctly (first forward then backward); however, sliderPageDelegate?.updateSong(index: self.lastPendingIndex, dir: 0) is only being called in the forward direction.
So, if we have list of songs (A, B, C, ...) and we are currently at song A. When the user swipes forward, the thumbnail changes to B's thumbnail and the player updates the song to B. However, if the forward swipe was followed by a backward swipe quickly, then the thumbnail changes to A but the song remains B
Update:
if A has index = 0 and B has index = 1, moving A->B->A quickly will print the following:
before completion : 1
before completion : 1
completed : 1
I have added PageControllerDelegate in your code and introduce two new methods. Then implement these delegates in your UIPageViewController class and then in PlayerImageViewController class invoke both methods in viewWillAppear and viewWillDisappear. Now remove you didFinishAnimation method and write that code in viewControllerIsBeingDisplay.
For deeper knowledge review this code.
class PlayerImageViewController: UIViewController {
var index: Int?
var image: UIImage?
weak var delegate: PageControllerDelegate?
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
if let del = self.delegate {
del.viewControllerIsBeingHide(self)
}
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if let del = self.delegate {
del.viewControllerIsBeingDisplay(self)
}
}
}
protocol SliderPageDelegate {
func updateSong(index: Int, dir: Int)
}
protocol PageControllerDelegate {
func viewControllerIsBeingHide(_ viewController: PlayerImageViewController)
func viewControllerIsBeingDisplay(_ viewController: PlayerImageViewController)
}
class SliderPgaeViewController: UIPageViewController, UIPageViewControllerDelegate, UIPageViewControllerDataSource, PageControllerDelegate {
var lastPendingIndex: Int = 0
var sliderPageDelegate: SliderPageDelegate? = nil
override func viewDidLoad() {
super.viewDidLoad()
self.dataSource = self
self.delegate = self
setViewControllers([createViewController(index: playerManager.getCurrentIndex())!], direction: .forward, animated: true, completion: nil)
lastPendingIndex = playerManager.getCurrentIndex()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func presentationCount(for pageViewController: UIPageViewController) -> Int {
return playerManager.getSongsCount()
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
if let vc = viewController as? PlayerImageViewController {
if (vc.index == 0){
return nil
}
return createViewController(index: vc.index! - 1)
}
return nil
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
if let vc = viewController as? PlayerImageViewController {
if (vc.index == playerManager.getSongsCount() - 1){
return nil
}
return createViewController(index: vc.index! + 1)
}
return nil
}
func pageViewController(_ pageViewController: UIPageViewController, willTransitionTo pendingViewControllers: [UIViewController]){
if let vc = pendingViewControllers[0] as? PlayerImageViewController {
self.lastPendingIndex = vc.index!
}
}
private func createViewController(index i: Int) -> UIViewController?{
let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "PlayerImageController") as! PlayerImageViewController
vc.index = i
vc.delegate = self
// vc.image = playerManager.getSong(index: i).image //Commented becoz i do not have this file
return vc
}
var previousVCIndex = 0
func viewControllerIsBeingHide(_ viewController: PlayerImageViewController) {
previousVCIndex = viewController.index!
}
func viewControllerIsBeingDisplay(_ viewController: PlayerImageViewController) {
if let del = sliderPageDelegate {
del.updateSong(index: previousVCIndex, dir: 0)
}
}
}
I am trying to auto-slide the page view controller pages and the page indicator simultaneously. There are 5 pages in my page view controller. The flow works fine from pic 1-5, but I am unable to go back from 5th to 1st image and restart the auto sliding.
Below is my code:
import UIKit
class PageSliderViewController: UIViewController , UIPageViewControllerDataSource, UIPageViewControllerDelegate {
#IBOutlet weak var pageControl: UIPageControl!
var pageContainer: UIPageViewController!
// The pages it contains
var pages = [UIViewController]()
// Track the current index
var currentIndex: Int?
private var pendingIndex: Int?
override func viewDidLoad() {
super.viewDidLoad()
// Setup the pages
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let page1: UIViewController! = storyboard.instantiateViewController(withIdentifier: "Page1")
let page2: UIViewController! = storyboard.instantiateViewController(withIdentifier: "Page2")
let page3: UIViewController! = storyboard.instantiateViewController(withIdentifier: "Page3")
let page4: UIViewController! = storyboard.instantiateViewController(withIdentifier: "Page4")
let page5: UIViewController! = storyboard.instantiateViewController(withIdentifier: "Page5")
pages.append(page1)
pages.append(page2)
pages.append(page3)
pages.append(page4)
pages.append(page5)
// Create the page container
pageContainer = UIPageViewController(transitionStyle: .scroll, navigationOrientation: .horizontal, options: nil)
pageContainer.delegate = self
pageContainer.dataSource = self
pageContainer.setViewControllers([page1], direction: UIPageViewControllerNavigationDirection.forward, animated: false, completion: nil)
currentIndex = 0
// Add it to the view
view.addSubview(pageContainer.view)
// Configure our custom pageControl
self.view.bringSubview(toFront: self.loginButton)
self.view.bringSubview(toFront: self.signUpButton)
self.view.bringSubview(toFront: self.pageControl)
pageControl.numberOfPages = pages.count
pageControl.currentPage = 0
Timer.scheduledTimer(timeInterval: 5,
target: self,
selector: #selector(self.next(_:)),
userInfo: nil,
repeats: true)
}
func next(_ timer: Timer) {
pageContainer.goToNextPage()
currentIndex = currentIndex! + 1
if currentIndex == pageControl.numberOfPages{
currentIndex = 0
}
pageControl.currentPage = currentIndex!
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - UIPageViewController delegates
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
let currentIndex = pages.index(of: viewController)!
if currentIndex == 0 {
return nil
}
let previousIndex = abs((currentIndex - 1) % pages.count)
return pages[previousIndex]
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
let currentIndex = pages.index(of: viewController)!
if currentIndex == pages.count-1 {
return nil
}
let nextIndex = abs((currentIndex + 1) % pages.count)
return pages[nextIndex]
}
func pageViewController(_ pageViewController: UIPageViewController, willTransitionTo pendingViewControllers: [UIViewController]) {
pendingIndex = pages.index(of: pendingViewControllers.first!)
}
func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
if completed {
currentIndex = pendingIndex
if let index = currentIndex {
pageControl.currentPage = index
}
}
}
}
extension UIPageViewController {
func goToNextPage(animated: Bool = true, completion: ((Bool) -> Void)? = nil) {
if let currentViewController = viewControllers?[0] {
if let nextPage = dataSource?.pageViewController(self, viewControllerAfter: currentViewController) {
setViewControllers([nextPage], direction: .forward, animated: animated, completion: completion)
}
}
}
}
Note that through this code the page control goes from 5th to 1st dot but the page is not changing. There is some minor mistake but unable to figure it out. Any help would be greatly appreciated!
I haven't tried to run this code, but my guess is:
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
let currentIndex = pages.index(of: viewController)!
if currentIndex == pages.count-1 {
return nil
}
let nextIndex = abs((currentIndex + 1) % pages.count)
return pages[nextIndex]
}
Arrays are zero-based, so if you are currently looking at page5, you are looking at pages[4] ... and pages.count - 1 equals 4. So, your code is saying:
if the current page is page5
return nil
So your func goToNextPage() extension calls for viewControllerAfter and gets nil in return.
You most likely want to change viewControllerAfter to be:
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
let currentIndex = pages.index(of: viewController)!
// you don't need this, because you are using % modulo operator
// to keep "nextIndex" within the array bounds
//if currentIndex == pages.count-1 {
// return nil
//}
let nextIndex = abs((currentIndex + 1) % pages.count)
return pages[nextIndex]
}