我正在尝试使用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)
}
这个approuch把我带到了这个错误:
由于未捕获的异常而终止应用 '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...
}
通过这个approuch,我得到一个空白的屏幕,使用调试器我发现数据源委托方法被调用(numberOfItemsInSection
和numberOfSectionsInCollectionView
),但没有其他委托方法被称为(sizeForItemAtIndexPath
和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 !!!
}
一切正常。
所以,我的问题是:有没有办法以AutoLayout编程方式使用UICollectionView?
解
过了一会儿,我发现我的问题是在约束中。然后我出来了这个解决方案:
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)
}
答案 0 :(得分:4)
过了一会儿,我发现我的问题是在约束中。然后我出来了这个解决方案:
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)
let stringConstraint = "V:|-\(slice)-[collection]-\(slice)-|"
print("stringConstraint: \(stringConstraint)")
constraints = NSLayoutConstraint.constraintsWithVisualFormat(stringConstraint, options: NSLayoutFormatOptions.AlignAllCenterX, metrics: nil, views: views)
self.view.addConstraints(constraints)
}