Ios UICollectionView-单元格不应为';不要在视图外滚动
情况:Ios UICollectionView-单元格不应为';不要在视图外滚动,ios,swift,xcode,uicollectionview,Ios,Swift,Xcode,Uicollectionview,情况: import UIKit class HomeTableViewController: UITableViewController, UICollectionViewDelegate, UICollectionViewDataSource { @IBOutlet weak var topSliderCollectionView: UICollectionView! var topSliderElement = [TopSliderElement]() func c
import UIKit
class HomeTableViewController: UITableViewController, UICollectionViewDelegate, UICollectionViewDataSource {
@IBOutlet weak var topSliderCollectionView: UICollectionView!
var topSliderElement = [TopSliderElement]()
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return topSliderElement.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "slider cell", for: indexPath) as! HomeSliderCollectionViewCell
let slider = topSliderElement[indexPath.row]
cell.titleLbl.text = slider.tile
cell.descriptionLBL.text = slider.description
return cell
}
}
我将UITableViewController
与故事板中设计的静态表格单元一起使用。在我的第一个UITableViewCell
中,我实现了一个漂亮的UICollectionView
但是有一个问题:当我滚动查看时,UICollectionViewCell
会移出帧。它们应该只停在正确的位置
代码:
import UIKit
class HomeTableViewController: UITableViewController, UICollectionViewDelegate, UICollectionViewDataSource {
@IBOutlet weak var topSliderCollectionView: UICollectionView!
var topSliderElement = [TopSliderElement]()
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return topSliderElement.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "slider cell", for: indexPath) as! HomeSliderCollectionViewCell
let slider = topSliderElement[indexPath.row]
cell.titleLbl.text = slider.tile
cell.descriptionLBL.text = slider.description
return cell
}
}
我试过这个:
这是一个可能的解决方案,但它不起作用,因为我不使用普通的UIViewController
问题:如何控制单元格,使其停在视图中的正确位置?您必须将单元格宽度设置为与
view.frame.width
相同,才能正常工作。如果单元格两侧有任何间距(确实如此),则必须将这些间距相加,然后从宽度中减去。即,view.frame.width-16
。完成此操作后,您必须在UICollectionView
中将isPaginEnabled
设置为true-这将在您停止滚动时将单元格锁定在屏幕中央。您必须将单元格宽度设置为与view.frame.width
相同,才能正常工作。如果单元格两侧有任何间距(确实如此),则必须将这些间距相加,然后从宽度中减去。即,view.frame.width-16
。完成此操作后,您必须在UICollectionView
中将isPaginEnabled
设置为true-当您停止滚动时,这会将单元格锁定在屏幕中央。您是否尝试过collectionView.isPaginEnabled=true?我不确定您的意思。您的帧是否小于屏幕的全宽且collectionview不遵守帧的限制?如果是这样,请尝试启用ViewClearification的“剪辑到边界”选项:卡片的大小非常完美。我只想让它们在滚动时反弹到正确的位置,这样它们在仅部分可见时不会停止。您是否尝试过collectionView.isPaginEnabled=true?我不确定您的意思。您的帧是否小于屏幕的全宽且collectionview不遵守帧的限制?如果是这样,请尝试启用ViewClearification的“剪辑到边界”选项:卡片的大小非常完美。我只想让它们在滚动时反弹到正确的位置,这样它们就不会在仅部分可见时停止