Swift 滑动导航 Table 浏览量

Swift Swipe Navigation Table Views

我正在尝试在 swift 中的不同 tableViewController 之间进行滑动导航。我设法用以下代码用 viewControllers 做到了:

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var scrollView: UIScrollView!

override func viewDidLoad() {
    super.viewDidLoad()

}

var scrollViewAdded = false

override func viewDidLayoutSubviews() {

    super.viewDidLayoutSubviews()

    if !scrollViewAdded {

        self.loadSrollView()

        self.scrollViewAdded = true
    }
}


func loadSrollView() {

    self.scrollView.pagingEnabled = true

    let vc0 = self.storyboard?.instantiateViewControllerWithIdentifier("ViewController0")

    self.addChildViewController(vc0!)
    self.scrollView.addSubview(vc0!.view)
    vc0!.didMoveToParentViewController(self)


    let vc1 = self.storyboard?.instantiateViewControllerWithIdentifier("ViewController1")

    var frame1 = vc1!.view.frame
    frame1.origin.x = self.view.frame.size.width
    vc1!.view.frame = frame1

    self.addChildViewController(vc1!)
    self.scrollView.addSubview(vc1!.view)
    vc1!.didMoveToParentViewController(self)


    let vc2 = self.storyboard?.instantiateViewControllerWithIdentifier("ViewController2")

    var frame2 = vc2!.view.frame
    frame2.origin.x = self.view.frame.size.width * 2
    vc2!.view.frame = frame2

    self.addChildViewController(vc2!)
    self.scrollView.addSubview(vc2!.view)
    vc2!.didMoveToParentViewController(self)


    let vc3 = self.storyboard?.instantiateViewControllerWithIdentifier("ViewController3")

    var frame3 = vc3!.view.frame
    frame3.origin.x = self.view.frame.size.width * 3
    vc3!.view.frame = frame3

    self.addChildViewController(vc3!)
    self.scrollView.addSubview(vc3!.view)
    vc3!.didMoveToParentViewController(self)


    self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width * 4, self.view.frame.size.height - 66)
}

}

我认为我可以通过用 tableviewcontrollers 替换 viewcontrollers 来调整它,但它不起作用。有人知道怎么做吗?

以下是在您的项目中使用 XLPagerTabStrip 库的步骤:

第1步:在Storyboard(Supposedly Intial VC)上拖一个VC,然后在这个VC上拖一个Container View(完全占据VC)。拖动另一个空 VC(将其 class 定义为 "myPagerStrip",这将在下一步中创建)。现在控制从容器视图中拖动并选择嵌入并将此情节提要的标识符定义为嵌入 segue 为 "showMyPagerStrip"

第 2 步: 使用以下代码创建一个名为 "myPagerStrip.swift" 的新 Swift 文件:-

注意: 对下面的代码有任何疑问,请参考上面已经提到的github link,那里都有解释。

import XLPagerTabStrip

class myPagerStrip: ButtonBarPagerTabStripViewController {


    override func viewDidLoad() {


        settings.style.buttonBarBackgroundColor = UIColor.yellowColor()


        // selected bar view is created programmatically so it's important to set up the following 2 properties properly
        settings.style.selectedBarBackgroundColor = UIColor.blackColor()
        //settings.style.selectedBarHeight: CGFloat = 5

        // each buttonBar item is a UICollectionView cell of type ButtonBarViewCell
        settings.style.buttonBarItemBackgroundColor =  UIColor.redColor()
        settings.style.buttonBarItemFont = UIFont.systemFontOfSize(18)
        // helps to determine the cell width, it represent the space before and after the title label
       // settings.style.buttonBarItemLeftRightMargin: CGFloat = 8
        settings.style.buttonBarItemTitleColor =  UIColor.whiteColor()
        // in case the barView items do not fill the screen width this property stretch the cells to fill the screen
        settings.style.buttonBarItemsShouldFillAvailiableWidth = true

        changeCurrentIndexProgressive = { (oldCell: ButtonBarViewCell?, newCell: ButtonBarViewCell?, progressPercentage: CGFloat, changeCurrentIndex: Bool, animated: Bool) -> Void in
            guard changeCurrentIndex == true else { return }

            oldCell?.label.textColor = UIColor(white: 1, alpha: 0.6)
            newCell?.label.textColor = .whiteColor()

            if animated {
                UIView.animateWithDuration(0.1, animations: { () -> Void in
                    newCell?.transform = CGAffineTransformMakeScale(1.0, 1.0)
                    oldCell?.transform = CGAffineTransformMakeScale(0.8, 0.8)
                })
            }
            else {
                newCell?.transform = CGAffineTransformMakeScale(1.0, 1.0)
                oldCell?.transform = CGAffineTransformMakeScale(0.8, 0.8)
            }
        }

        super.viewDidLoad()
    }


    override func viewControllersForPagerTabStrip(pagerTabStripController: PagerTabStripViewController) -> [UIViewController] {


        let child_1 = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("tableViewOne") // First Table View

        let child_2 = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("tableViewTwo") // Second Table View

        let child_3 = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("tableViewThree") // Third Table View

        let childVC = [child_1,child_2, child_3]

        return childVC

    }

    override func reloadPagerTabStripView() {
        super.reloadPagerTabStripView()
    }

}

第 3 步: 在 Storyboard 上再拖 3 个 VC 并创建 3 个 Cocoa Class 文件,即 tableViewOne.swift、tableViewTwo.swift 和 tableViewThree.swift 的故事板 ID 为 "tableViewOne","tableViewTwo", "tableViewThree".//(如上面代码中提到的 child_1,child_2 和 child_3)

注意: 假设所有 3 个 table 视图或所需的 table 视图都设置了所需的数据。

第 4 步: 在每个 table 视图中继承 "IndicatorInfoProvider"(在第 5 步之后执行)即

class tableViewOne: UIViewController,IndicatorInfoProvider

第 5 步: 在每个文件中导入 XLPagerTabStrip,并在每个 tableView 中添加以下函数:-

func indicatorInfoForPagerTabStrip(pagerTabStripController: PagerTabStripViewController) -> IndicatorInfo {
        return IndicatorInfo(title: "My Child title 1")
    }

现在,你已经完成了。只需 运行 模拟器或实际设备中的项目。:)

如果 table 视图有问题,以下是 tableViewOne.swift 的代码供参考:-

import UIKit
import XLPagerTabStrip

class tableViewOne: UIViewController,IndicatorInfoProvider {

    @IBOutlet var tableView: UITableView!
        var dummyData = ["data 0","data 001","data try"]

    override func viewDidLoad() {
        super.viewDidLoad()

    }

    func indicatorInfoForPagerTabStrip(pagerTabStripController: PagerTabStripViewController) -> IndicatorInfo {
        return IndicatorInfo(title: "My Child title 1")
    }

}
extension tableViewOne: UITableViewDataSource, UITableViewDelegate {

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        return dummyData.count
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
    {
        let cell = tableView.dequeueReusableCellWithIdentifier("Cell")! as UITableViewCell

        cell.textLabel!.text = dummyData[indexPath.row]

        return cell;
    }

}