我在另一个控制器中添加了UICollectionView
作为子视图。我为它设置了框架,UICollectionView
流程是水平的。我需要只显示1行项目并按水平滚动。但我垂直得到几行。我尝试了几种不同的方法,但它对我没有帮助。我搜索了有关它的信息,但这些方法也没有帮助我。我该如何解决?
我的代码
override func viewDidLoad() {
super.viewDidLoad()
addUIElements()
}
// MARK: - UI functions
private func addUIElements() {
view.addSubview(headerView)
let lifelineController = StoryboardManager.lifeLineStoryboard.instantiateViewControllerWithIdentifier("LifeLineCollectionViewController") as! LifeLineCollectionViewController
addChildViewController(lifelineController)
lifelineController.view.frame = CGRect(x: 0, y: headerView.frame.height, width: headerView.frame.width, height: 100)
view.addSubview(lifelineController.view)
lifelineController.didMoveToParentViewController(self)
}
布局代码
extension LifeLineCollectionViewController: UICollectionViewDelegateFlowLayout {
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
let value = 50
return CGSize(width: value, height: value)
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat {
return 8
}
// func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
// return CGSize(width: collectionView.frame.width, height: 376)
// }
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets {
let top: CGFloat = 48
let cornerValue: CGFloat = 8
let bottom: CGFloat = 40
return UIEdgeInsets(top: top, left: cornerValue, bottom: bottom, right: cornerValue)
}
}
我在viewDidAppear的UICollectionView中打印了它的框架,我获得了一个完全不同的结果508高度,但我将136设置为高度。
答案 0 :(得分:0)
我在SnapKit的帮助下解决了它
private func addUIElements() {
view.addSubview(headerView)
let lifelineController = StoryboardManager.lifeLineStoryboard.instantiateViewControllerWithIdentifier("LifeLineCollectionViewController") as! LifeLineCollectionViewController
addChildViewController(lifelineController)
view.addSubview(lifelineController.view)
lifelineController.view.snp_makeConstraints { (make) in
make.width.equalTo(headerView.frame.width)
make.height.equalTo(136)
make.centerX.equalTo(view.snp_centerX)
make.top.equalTo(headerView.snp_bottom)
}
lifelineController.didMoveToParentViewController(self)
}