我的要求是在 ViewController 中的视图之上有一个滑块集合视图。我已经尝试了很多解决方法来满足我的要求(您可以在我的集合视图设置代码中看到)。但是,当我收到关于The behavior of the UICollectionViewFlowLayout is not defined
. 视图控制器是从控制器数组中调用的,而控制器数组UITabBarController
又嵌入在UINavigtionController
. 视图控制器包含一个高度为 200 的集合视图。我希望集合视图的每个单元格都扩展其完整的宽度和高度。
警告:
2017-09-06 12:05:19.139 collectionViewSample[3686:79491] The behavior of the UICollectionViewFlowLayout is not defined because:
2017-09-06 12:05:19.139 collectionViewSample[3686:79491] the item height must be less than the height of the UICollectionView minus the section insets top and bottom values, minus the content insets top and bottom values.
2017-09-06 12:05:19.140 collectionViewSample[3686:79491] The relevant UICollectionViewFlowLayout instance is <UICollectionViewFlowLayout: 0x7ff4e4c00020>, and it is attached to <UICollectionView: 0x7ff4e5032600; frame = (0 64; 414 200); clipsToBounds = YES; gestureRecognizers = <NSArray: 0x600000047290>; layer = <CALayer: 0x60000002d240>; contentOffset: {0, -64}; contentSize: {0, 0}> collection view layout: <UICollectionViewFlowLayout: 0x7ff4e4c00020>.
2017-09-06 12:05:19.140 collectionViewSample[3686:79491] Make a symbolic breakpoint at UICollectionViewFlowLayoutBreakForInvalidSizes to catch this in the debugger.
应用委托:
window = UIWindow(frame: UIScreen.main.bounds)
window?.makeKeyAndVisible()
let mainController = UITabBarController()
let navController = UINavigationController(rootViewController: ViewController())
mainController.viewControllers = [navController]
window?.rootViewController = mainController
视图控制器:
class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
setupCollectionView()
}
func setupCollectionView() {
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .horizontal
let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
collectionView.translatesAutoresizingMaskIntoConstraints = false
collectionView.scrollIndicatorInsets = .zero
collectionView.contentInset = .zero
collectionView.preservesSuperviewLayoutMargins = false
collectionView.layoutMargins = .zero
collectionView.delegate = self
collectionView.dataSource = self
collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "cellId")
view.addSubview(collectionView)
collectionView.topAnchor.constraint(equalTo: topLayoutGuide.bottomAnchor).isActive = true
collectionView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
collectionView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
collectionView.heightAnchor.constraint(equalToConstant: 200).isActive = true
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath)
cell.backgroundColor = .green
return cell
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 2
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsetsMake(0, 0, 0, 0)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: view.frame.width, height: 200)
}
}
当单元格填充了填充集合视图的整个框架的图像时,它会在导航栏的顶部留下一个小间隙。集合视图框架确实扩展到我的要求,但单元格没有。我将如何以正确的方式解决这个问题?