[英]Swift Swipe Navigation Table Views
我正在嘗試快速在不同的tableViewControllers之間進行滑動導航。 我設法用以下代碼的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來適應它,但是它不起作用。 有人知道該怎么做嗎?
步驟1:在Storyboard上拖動一個VC(據說是Intial VC),然后在該VC上拖動一個Container View(完全占據該VC)。 拖動另一個空的VC(將其類定義為“ myPagerStrip”,這將在下一步中創建)。 現在,從“容器視圖”中拖動“控制”,然后選擇“嵌入”,並將此情節提要嵌入標記的標識符定義為“ showMyPagerStrip”
步驟2:使用以下代碼創建一個名為“ myPagerStrip.swift”的新Swift文件:
注意:如果對下面的代碼有任何疑問,請參考上面已經提到的github鏈接,那里有所有解釋。
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:再將3個VC拖到Storyboard上,並創建3個Cocoa Class文件,即tableViewOne.swift,tableViewTwo.swift和tableViewThree.swift,其Storyboard ID為“ tableViewOne”,“ tableViewTwo”,“ tableViewThree”。//(如上所述child_1,child_2和child_3的代碼)
注意:假設所有3個tableViews或必需的tableViews都設置了必需的數據。
步驟4:在每個tableViews中繼承“ IndicatorInfoProvider”(在步驟5之后執行),即
class tableViewOne: UIViewController,IndicatorInfoProvider
步驟5:在每個文件中導入XLPagerTabStrip,並在每個tableView中添加以下功能:
func indicatorInfoForPagerTabStrip(pagerTabStripController: PagerTabStripViewController) -> IndicatorInfo {
return IndicatorInfo(title: "My Child title 1")
}
現在,您完成了。 只需在Simulator或Actual設備中運行項目即可。
如果遇到表視圖問題,下面是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;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.