繁体   English   中英

UIPageViewController 不使用 storyboard

[英]UIPageViewController using without storyboard

我有一个 api 如下:-

{
"datashow": [
{
  "name": "seena",
  "age": 34,
  "city": "u.k",
  "type": "human"
},
{
  "name": "cat",
  "age": 3,
  "city": "u.s.a",
  "type": "animal"
},
{
  "name": "parrot",
  "age": 31,
  "city": "south africa",
  "type": "bird"
}
],
  "link": {}
}

这是一个例子所以,我只需要获取“datashow”的计数。所以这里的计数是 3。所以,现在我需要为此创建UIPageViewController

这意味着我需要 3 个带有 pagecontrol 的屏幕。在第一个屏幕中,它应该显示详细信息,如 seena, 34, uk in label。然后在第二个屏幕中,它应该显示为 cat,3,usa,第三个屏幕也显示相同的模式。

这该怎么做? 它不能使用 storyboard 来显示它可以使用xib的屏幕UIViewController swift怎么办?

这是所有 ViewController 代码

import UIKit

extension UIColor { static func random() -> UIColor { return UIColor(red: .random(in: 0...1),green: .random(in: 0...1),blue: .random(in: 0...1), 阿尔法: 1.0 ) } }

class ViewController: UIViewController,UIPageViewControllerDataSource, UIPageViewControllerDelegate, UIScrollViewDelegate {

@IBOutlet weak var topTabCollectionView:UICollectionView!
@IBOutlet weak var pagesContainerView:UIView!

private var pageController: UIPageViewController!
private var arrVC:[UIViewController] = []
private var currentPage: Int!

var numberOfTabArr = ["All","Movie","Songs","TV Serials","WebSeriese","News","Romantic","Recent Uploaded",]


override func viewDidLoad() {
    super.viewDidLoad()
    
    supposeYourTabDataGetHere()
    currentPage = 0
    createPageViewController()
    
    topTabCollectionView.dataSource = self
    topTabCollectionView.delegate = self
    
}

func supposeYourTabDataGetHere(){
    
    for name in numberOfTabArr {
        let vc1 = storyboard?.instantiateViewController(withIdentifier: "ListControllerVC") as! ListControllerVC
        vc1.tabName = name
        vc1.TabId = "someID"
        vc1.color = UIColor.random()
        arrVC.append(vc1)
    }
    
}

private func createPageViewController() {
    
    pageController = UIPageViewController.init(transitionStyle: UIPageViewController.TransitionStyle.scroll, navigationOrientation: UIPageViewController.NavigationOrientation.horizontal, options: nil)
    
    pageController.view.backgroundColor = UIColor.clear
    pageController.delegate = self
    pageController.dataSource = self
    
    for svScroll in pageController.view.subviews as! [UIScrollView] {
        svScroll.delegate = self
    }
    
    DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
        self.pageController.view.frame = self.pagesContainerView.bounds
        
    }
    pageController.setViewControllers([arrVC[0]], direction: UIPageViewController.NavigationDirection.forward, animated: false, completion: nil)
    
    self.addChild(pageController)
    self.pagesContainerView.addSubview(pageController.view)
    pageController.didMove(toParent: self)
}

private func indexofviewController(viewCOntroller: UIViewController) -> Int {
    if(arrVC .contains(viewCOntroller)) {
        return arrVC.firstIndex(of: viewCOntroller)!
    }
    return -1
}




//MARK: - Pagination Delegate Methods

func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
    var index = indexofviewController(viewCOntroller: viewController)
    if(index != -1) {
        index = index - 1
    }
    if(index < 0) {
        return nil
    }
    else {
        return arrVC[index]
    }
}

func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
    var index = indexofviewController(viewCOntroller: viewController)
    if(index != -1) {
        index = index + 1
    }
    if(index >= arrVC.count) {
        return nil
    }
    else {
        return arrVC[index]
    }
}


func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
    
    if(completed) {
        currentPage = arrVC.firstIndex(of: (pageViewController.viewControllers?.last)!)
        topTabCollectionView.reloadData()
        let indexpath = IndexPath(row: currentPage, section: 0)
        topTabCollectionView.scrollToItem(at: indexpath, at: .centeredHorizontally, animated: true)
        
    }
    
}

}

//对于 TAB 扩展 ViewController:UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout{ func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return numberOfTabArr.count }

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
    
    let lbl = cell.contentView.viewWithTag(11) as? UILabel
    cell.layer.cornerRadius = 4.0
    
    lbl?.text = numberOfTabArr[indexPath.row]
    
    if indexPath.row == currentPage {
        cell.backgroundColor = .white
    }else{
        cell.backgroundColor = .clear
    }
    
    return cell
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    
    let label = UILabel(frame: CGRect.zero)
    label.text = numberOfTabArr[indexPath.row]
    label.sizeToFit()
    return CGSize(width: label.frame.size.width+15, height: 32)
}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    
    if currentPage>indexPath.row{
        pageController.setViewControllers([arrVC[indexPath.row]], direction: UIPageViewController.NavigationDirection.reverse, animated: true, completion: nil)
        currentPage = indexPath.row
        topTabCollectionView.reloadData()
        
        topTabCollectionView.scrollToItem(at: indexPath, at: .centeredHorizontally, animated: true)
    }
    else if currentPage<indexPath.row{
        pageController.setViewControllers([arrVC[indexPath.row]], direction: UIPageViewController.NavigationDirection.forward, animated: true, completion: nil)
        currentPage =  indexPath.row
        topTabCollectionView.reloadData()
        
        topTabCollectionView.scrollToItem(at: indexPath, at: .centeredHorizontally, animated: true)
    }else{
        //already at same index
    }
    
    
}

}

我不确定你为什么要使用 xib,而不是 storyboard。Storyboard 会让你比 xib 更有能力。

但是,这一定对你有帮助https://github.com/Satish/OnboardingPageViewController/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM