Saya baru mengenal pemrograman di Xcode dan saya bertanya-tanya bagaimana saya bisa menambahkan halaman baru ke UIPageViewController saya? Saya mengikuti tutorial youtube untuk mencapai ini. Di IOS Simulator berfungsi dengan baik tetapi saya hanya ingin menambahkan halaman lain, saya memiliki storyboard dengan ID "ketiga" itu adalah halaman yang ingin saya tambahkan, saya telah mencoba menambahkan storyboard ketiga sebagai viewController baru, tambahkan beberapa kode di bagian viewControllerAfter tetapi kodenya akhirnya rusak, adakah yang bisa membantu saya?

 import UIKit

class PageViewController: UIPageViewController, UIPageViewControllerDelegate, UIPageViewControllerDataSource {

    lazy var orderedviewControllers: [UIViewController] = {
        return [self.newVc(viewController: "first"),
                self.newVc(viewController: "second")]
    }()

    var pageControl = UIPageControl()

    override func viewDidLoad() {
        super.viewDidLoad()

        self.dataSource = self
        if let firstViewController = orderedviewControllers.first {
            setViewControllers([firstViewController],
                               direction: .forward,
                               animated: true,
                               completion: nil)
        }

        self.delegate = self
        configurePageControl()

        // Do any additional setup after loading the view.
    }

    func configurePageControl() {
        pageControl = UIPageControl(frame: CGRect(x: 0, y: UIScreen.main.bounds.maxY - 50, width: UIScreen.main.bounds.width, height: 50))
        pageControl.numberOfPages = orderedviewControllers.count
        pageControl.currentPage = 0
        pageControl.tintColor = UIColor.black
        pageControl.pageIndicatorTintColor = UIColor.white
        pageControl.currentPageIndicatorTintColor = UIColor.black
        self.view.addSubview(pageControl)
    }
    func newVc(viewController: String) -> UIViewController {
        return UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: viewController)
    }

    func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
        guard let viewControllerIndex = orderedviewControllers.index(of: viewController) else {
            return nil
        }

        let previousIndex  = viewControllerIndex - 1

        guard previousIndex >= 0 else {
            return orderedviewControllers.last
        }
        guard orderedviewControllers.count > previousIndex else {
            return nil
        }
        return orderedviewControllers[previousIndex]
    }

    func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
         guard let viewControllerIndex = orderedviewControllers.index(of: viewController) else {
                   return nil
               }

               let nextIndex  = viewControllerIndex + 1

        guard orderedviewControllers.count != nextIndex else {
                return orderedviewControllers.first

               }
               guard orderedviewControllers.count > nextIndex else {
                   return nil
               }

               return orderedviewControllers[nextIndex]
                   }

    func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
        let pageContentViewController = pageViewController.viewControllers![0]
        self.pageControl.currentPage = orderedviewControllers.index(of: pageContentViewController)!

    }

    }
0
Anthony 11 April 2020, 15:18

1 menjawab

Jawaban Terbaik

Anda dapat menggunakan kode di bawah ini untuk mengatur pengontrol halaman Anda, menambahkan pengontrol tampilan baru di larik halaman. Ini tidak dilakukan dengan menggunakan storyboard tetapi mungkin bisa membantu tambahan

class PageController: UIViewController {

var pages = [UIViewController]()

override func viewDidLoad() {
    super.viewDidLoad()

    let pageViewController = UIPageViewController(transitionStyle: .scroll, navigationOrientation: .horizontal, options: .none)
    view.backgroundColor = .clear
    pageViewController.dataSource = self
    pageViewController.delegate = self

    let initialPage = 0
    let page1 = CustomController1()
    let page2 = CustomController2()
    let page3 = CustomController3()

    // add the individual viewControllers to the pageViewController
    self.pages.append(page1)
    self.pages.append(page2)
    self.pages.append(page3)

    let pageControllerView = pageViewController.view
    view.addSubview(pageControllerView!)
    addChild(pageViewController)


    let dictionary: [String: UIView] = ["pageControllerView": pageControllerView!]
    var vflString = "H:|-0-[pageControllerView]-0-|"
    var vflConstraints = NSLayoutConstraint.constraints(withVisualFormat: vflString, options: [], metrics: nil, views: dictionary)
    NSLayoutConstraint.activate(vflConstraints)

    vflString = "V:|-[pageControllerView]-|"
    vflConstraints = NSLayoutConstraint.constraints(withVisualFormat: vflString, options: [], metrics: nil, views: dictionary)
    NSLayoutConstraint.activate(vflConstraints)


// EDIT:

// Add new view controller on tapping a a button
let button = UIButton(frame: .zero)
    button.setTitle("This is a button", for: .normal)
    button.translatesAutoresizingMaskIntoConstraints = false
    page1.view.addSubview(button)
    button.addTarget(self, action: #selector(addPage), for: .touchUpInside)

    var vflString = "V:|-100-[button(50)]"
    var vflConstraints = NSLayoutConstraint.constraints(withVisualFormat: vflString,
                                                        options: [],
                                                        metrics: nil,
                                                        views: ["button": button])

    page1.view.addConstraints(vflConstraints)

    vflString = "H:|-100-[button(50)]"
    vflConstraints = NSLayoutConstraint.constraints(withVisualFormat: vflString,
                                                    options: [],
                                                    metrics: nil,
                                                    views: ["button": button])
    page1.view.addConstraints(vflConstraints)

    pageViewController.setViewControllers([pages[initialPage]], direction: .forward, animated: true, completion: nil)

}
@objc private func addPage() {
    let page2 = UIViewController()
    page2.view.backgroundColor = .brown
    pages.append(page2)
}
}

extension PageController: UIPageViewControllerDataSource, UIPageViewControllerDelegate {



func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
    if let viewControllerIndex = self.pages.firstIndex(of: viewController) {
        if viewControllerIndex == 0 {
            // wrap to last page in array
            return self.pages.last
        } else {
            // go to previous page in array
            return self.pages[viewControllerIndex - 1]
        }
    }
    return nil
}

func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
    if let viewControllerIndex = self.pages.firstIndex(of: viewController) {
        if viewControllerIndex < self.pages.count - 1 {
            // go to next page in array
            return self.pages[viewControllerIndex + 1]
        } else {
            // wrap to first page in array
            return self.pages.first
        }
    }
    return nil
}

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

    // set the pageControl.currentPage to the index of the current viewController in pages
    if let viewControllers = pageViewController.viewControllers {
        if let viewControllerIndex = self.pages.firstIndex(of: viewControllers[0]) {
            self.pageControl.currentPage = viewControllerIndex
        }
    }
}

// The number of items reflected in the page indicator.
func presentationCount(for pageViewController: UIPageViewController) -> Int {

}

// The selected item reflected in the page indicator.
func presentationIndex(for pageViewController: UIPageViewController) -> Int {
    return 1
}
}
0
Manoj 12 April 2020, 13:53