【发布时间】:2017-04-26 09:04:09
【问题描述】:
因为我已经为集合视图提供了自动布局,但纵向它很好:
问题在于,随着景观的发展,细胞之间的差距正在增加。如何避免这种情况?
import UIKit
class ViewController: UIViewController,UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout{
@IBOutlet weak var collectionView: UICollectionView!
var images = ["apple","banana","cherry","grapes","kiwifruit","mangoes","orange","papaya","passionfruit","peaches","pineapple","strawberry","sugarapple","watermelon"]
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(orientationChanged(notification:)), name: Notification.Name.UIDeviceOrientationDidChange, object: nil)
collectionView.dataSource = self
collectionView.delegate = self
}
func orientationChanged(notification: Notification) {
collectionView.collectionViewLayout.invalidateLayout()
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return images.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "stridentifier",for:indexPath) as! FruitsCollectionViewCell
cell.image.image = UIImage(named: images[indexPath.row])
cell.nameLabel.text = images[indexPath.row]
return cell
}
class SampleCollectionViewFlowLayout: UICollectionViewFlowLayout {
override init() {
super.init()
setUpLayout()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setUpLayout()
}
override var itemSize: CGSize {
set {}
get {
let itemWidth = ((self.collectionView?.bounds.width)! / 3.0) - self.minimumLineSpacing - minimumInteritemSpacing
return CGSize(width: itemWidth, height: itemWidth)
}
}
func setUpLayout() {
minimumInteritemSpacing = 0
minimumLineSpacing = 1.0
scrollDirection = .vertical
}
override func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool {
return true
}
}
}
【问题讨论】:
-
你是使用 UICollectionViewFlowLayout 还是继承了它?
-
你修复了 CollectionView 中的单元格大小吗?
-
不,我没有使用 uicollectionviewflowlayout
-
请详细解释您的问题,您尝试了什么,这是什么? UITableVIew,UICollectionView,UIScrollView?。如果您为自动布局编写了任何代码,请添加有问题的代码。
标签: ios swift swift3 autolayout