My requirement is to have a slider collection view on top of my view in ViewController. I've tried many work around for my requirement (You can see that in my collection view setup code). But, time I receive the warning about The behavior of the UICollectionViewFlowLayout is not defined
. The view controller is being called from controllers array inside of a UITabBarController
which in turn is embedded in a UINavigtionController
. The view controller contains a collection view of height 200. I want each cell of the collection view to expand its full width and height.
Warning:
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)
}
}
When the cells are populated with image that fills the entire frame of the collection view it leaves a small gap on top from navigation bar. Collection view frame does extend to my requirements but the cells are not. How would I fix this the proper way?