【发布时间】:2016-02-19 15:04:14
【问题描述】:
我正在尝试使用 AutoLayout 以编程方式创建 UICollectionView,但遇到了一些问题。
第一次测试:
在我的第一次尝试中,我创建了一个 UICollectionView 作为 Lazy var,并将其添加到 viewDidLoad 中,如下所示:
lazy var collection: UICollectionView = {
let cv = UICollectionView()
cv.translatesAutoresizingMaskIntoConstraints = false
cv.setCollectionViewLayout(self.flowLayout, animated: true)
cv.dataSource = self
cv.delegate = self
cv.registerClass(MyViewCell.self, forCellWithReuseIdentifier: "cell")
return cv
}()
override func viewDidLoad() {
super.viewDidLoad()
self.view.addSubview(self.collection)
}
这个方法把我带到了这个错误:
由于未捕获的异常而终止应用程序 'NSInvalidArgumentException',原因:'UICollectionView 必须是 用非零布局参数初始化'
第二次测试:
所以我不得不使用 frame/collectionViewLayout 初始化程序,而我的第二次尝试看起来像这样:
lazy var collection: UICollectionView = {
let cv = UICollectionView(frame: UIScreen.mainScreen().bounds, collectionViewLayout: self.flowLayout)
cv.translatesAutoresizingMaskIntoConstraints = false
cv.setCollectionViewLayout(self.flowLayout, animated: true)
cv.dataSource = self
cv.delegate = self
cv.registerClass(MenuViewCell.self, forCellWithReuseIdentifier: "cell")
return cv
}()
override func viewDidLoad() {
super.viewDidLoad()
self.view.addSubview(self.collection)
//Constraints was there...
}
通过这个方法我得到一个空白屏幕,使用调试器我发现 数据源委托方法 被调用(numberOfItemsInSection 和 numberOfSectionsInCollectionView),但没有调用其他委托方法(@ 987654325@和cellForItemAtIndexPath)
所以我决定摆脱 autoLayout 并看看事情的运作方式。
第三次测试:
lazy var collection: UICollectionView = {
let cv = UICollectionView(frame: UIScreen.mainScreen().bounds, collectionViewLayout: self.flowLayout)
// cv.translatesAutoresizingMaskIntoConstraints = false
cv.setCollectionViewLayout(self.flowLayout, animated: true)
cv.dataSource = self
cv.delegate = self
cv.registerClass(MenuViewCell.self, forCellWithReuseIdentifier: "cell")
return cv
}()
override func viewDidLoad() {
super.viewDidLoad()
self.view.addSubview(self.collection)
//No more constraints here !!!
}
一切正常。
所以,我的问题是:有没有办法以编程方式将 UICollectionView 与 AutoLayout 结合使用?
解决方案
过了一会儿,我发现我的问题出在约束上。然后我想出了这个解决方案:
let slice = (UIScreen.mainScreen().bounds.height / CGFloat(3.0))
lazy var flowLayout:UICollectionViewFlowLayout = {
let f = UICollectionViewFlowLayout()
f.scrollDirection = UICollectionViewScrollDirection.Horizontal
return f
}()
lazy var collection: UICollectionView = {
let cv = UICollectionView(frame: CGRectZero, collectionViewLayout: self.flowLayout)
cv.translatesAutoresizingMaskIntoConstraints = false
cv.setCollectionViewLayout(self.flowLayout, animated: true)
cv.dataSource = self
cv.delegate = self
cv.registerClass(MenuViewCell.self, forCellWithReuseIdentifier: "cell")
return cv
}()
override func viewDidLoad() {
super.viewDidLoad()
self.view.addSubview(self.collection)
let views = ["collection":self.collection]
var constraints = NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[collection]-0-|", options: NSLayoutFormatOptions.AlignAllTop, metrics: nil, views: views)
self.view.addConstraints(constraints)
constraints = NSLayoutConstraint.constraintsWithVisualFormat("V:|-\(slice)-[collection]-\(slice)-|", options: NSLayoutFormatOptions.AlignAllCenterX, metrics: nil, views: views)
self.view.addConstraints(constraints)
}
【问题讨论】:
-
太棒了,非常感谢
标签: ios swift autolayout uicollectionview