我的要求是在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.
的AppDelegate:
window = UIWindow(frame: UIScreen.main.bounds)
window?.makeKeyAndVisible()
let mainController = UITabBarController()
let navController = UINavigationController(rootViewController: ViewController())
mainController.viewControllers = [navController]
window?.rootViewController = mainController
的ViewController:
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)
}
}
当单元格填充了填充集合视图的整个框架的图像时,它会在导航栏的顶部留下一个小间隙。集合视图框架确实扩展到我的要求,但细胞不是。我该如何以正确的方式解决这个问题?