I'm developing an application without storyboards/xibs, which is proving to be a challenge. I have a UICollectionView (of ads) where users can tap on a cell to navigate to a new view controller to show details for that ad. Below is a gif of the error I'm having.
As you can see, the UICollectionView kind of sticks around after I tap the cell. I have a adDetailsViewController property in my AdsViewController subclass, and when I tap the cell this code executes:
adDetailsViewController.adTitle = adTitles[indexPath.row]
navigationController?.pushViewController(adDetailsViewController, animated: true)
Below are the life cycle methods (currently just viewDidLoad) of adDetailsViewController:
class AdDetailsViewController: UIViewController {
var adTitle: String?
override func viewDidLoad() {
super.viewDidLoad()
let label = UILabel(frame: view.bounds)
label.textAlignment = .center
label.center = view.center
label.text = adTitle
view.addSubview(label)
}
}
For completeness, here's the rest of AdsViewController:
class AdsViewController: UIViewController {
lazy var collectionView: AdsCollectionView = {
let collectionView = AdsCollectionView(frame: view.frame, collectionViewLayout: TiledFlowLayout())
collectionView.delegate = self
collectionView.dataSource = self
collectionView.register(AdCollectionViewCell.self, forCellWithReuseIdentifier: "AdCell")
view.addSubview(collectionView)
return collectionView
}()
let adDetailsViewController = AdDetailsViewController()
let adTitles = ["1", "2", "3", "4", "5", "6"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
collectionView.translatesAutoresizingMaskIntoConstraints = false
collectionView.topAnchor.constraint(equalTo: view.topAnchor, constant: 8).isActive = true
collectionView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0).isActive = true
collectionView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0).isActive = true
collectionView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 0).isActive = true
view.backgroundColor = .white
}
}
extension AdsViewController: UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return adTitles.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "AdCell", for: indexPath) as! AdCollectionViewCell
cell.textLabel.text = adTitles[indexPath.row]
return cell
}
}
extension AdsViewController: UICollectionViewDelegate {
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
adDetailsViewController.adTitle = adTitles[indexPath.row]
navigationController?.pushViewController(adDetailsViewController, animated: true)
}
}
extension AdsViewController: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: (self.view.frame.size.width / 2) - 10, height: self.view.frame.size.height / 4)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsets(top: 0, left: 5, bottom: 0, right: 5)
}
}
Ok, I fixed it. It seems that if I set the backgroundColor of AdDetailsViewController to UIColor.white, then it transitions as expected.
You have to share more of your code. Are there any special settings f.e.? Like a transparent background? Anything you do in viewDidLoad? Did you overwrite viewWillAppear, viewDidAppear, etc. and did not call the subclass functions (like super.viewWillAppear(animated))? That's a very common mistake...
Related
I am attempting to resize the collectionView cell inside the masterViewController of a SplitViewController but the resizing did not work. I have implemented the necessary viewWillTransition and invalidateLayout and it turns out fine if it isn't housed inside a splitViewController. In my case, I have full screen cell size.
Here's my implementation:
//This CollectionController is housed inside a TabBarController.
//The TabBarController is the masterViewController
class CollectionController: UIViewController, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {
lazy var collectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
let cv = UICollectionView(frame: CGRect.zero, collectionViewLayout: layout)
cv.translatesAutoresizingMaskIntoConstraints = false
cv.backgroundColor = .gray
cv.dataSource = self
cv.delegate = self
layout.scrollDirection = .horizontal
cv.isPagingEnabled = true
cv.showsHorizontalScrollIndicator = false
cv.showsVerticalScrollIndicator = false
cv.contentInsetAdjustmentBehavior = .always
cv.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "cell")
return cv
}()
var spacing: CGFloat = 16
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(collectionView)
collectionView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
collectionView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
collectionView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
collectionView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 3
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
cell.backgroundColor = .blue
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 16
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 16
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let layoutFrame = view.safeAreaLayoutGuide.layoutFrame
let size = CGSize(width: layoutFrame.width, height: layoutFrame.height)
print("SizeForItem", size)
return setUpCellSize(size: size)
}
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)
if let layout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout {
print("Transit1", size)
layout.itemSize = setUpCellSize(size: size) //With or without this makes no difference
layout.invalidateLayout()
print("Transit2", size)
}
}
func setUpCellSize(size: CGSize) -> CGSize {
return size
}
}
This is the result if implemented purely in the TabBarController, ie not inside a splitViewController. Noticed that cells (in blue) are positioned well beneath the status bar. Result is as what is expected and desired.
This is the result if implemented inside a splitViewController. Noticed that on first rotation to landscape, the cells went above the status bar. And when rotated back to portrait, the cell did not resize back to full screen.
Is there something else that needs to be implemented if the collectionView is placed inside a masterViewController?
EDIT
I add print statements inside sizeForItem and viewWillTransition. It turns out that on second rotation, the new size in viewWillTransition were not passed to the sizeForItem.
Even explicit calls to setupCellSize didn't update correctly. It also give the The behavior of the UICollectionViewFlowLayout is not defined warning. See gif (Gif starts when iPad is in landscape mode).
Also notice that when iPad rotates from landscape -> portrait, blue cell anchors to bottom of statusBar and top of tabBar. This is desired. Then when rotated back from portrait -> landscape, blue cell exceeds statusBar and went underneath tabBar. This is not what I want.
You need to embed the CollectionController (the MASTER CONTROLLER) in a UINavigationController. If you really don't want a navigationBar, just hide it.
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.navigationBar.isHidden = true
}
Ok, I have managed to solve this issue. #Glenn's solution helped partially. The portion that helped resolved this entirely is from this post.
First, implement #Glenn's solution on embedding CollectionController in a UINavigationController and hiding the navBar (since I do not want the navBar).
Second, add invalidateLayout in viewWillLayoutSubviews.
Full Code:
class CollectionController: UIViewController, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {
lazy var collectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .horizontal
let cv = UICollectionView(frame: CGRect.zero, collectionViewLayout: layout)
cv.translatesAutoresizingMaskIntoConstraints = false
cv.backgroundColor = .gray
cv.dataSource = self
cv.delegate = self
cv.isPagingEnabled = true
cv.alwaysBounceHorizontal = true
cv.showsHorizontalScrollIndicator = false
cv.showsVerticalScrollIndicator = false
cv.contentInsetAdjustmentBehavior = .always
cv.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "cell")
return cv
}()
var spacing: CGFloat = 16
//FIRST
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
navigationController?.navigationBar.isHidden = true
}
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(collectionView)
collectionView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
collectionView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
collectionView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
collectionView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
cell.backgroundColor = .blue
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let layoutFrame = view.safeAreaLayoutGuide.layoutFrame
let size = CGSize(width: layoutFrame.width, height: layoutFrame.height)
return setUpCellSize(size: size)
}
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)
collectionView.collectionViewLayout.invalidateLayout()
}
//SECOND
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
collectionView.collectionViewLayout.invalidateLayout()
}
func setUpCellSize(size: CGSize) -> CGSize {
return size
}
}
Result like so, blue cell anchored nicely to the bottom of statusBar and top of tabBar.
A side note, I attempted some of the solution in this thread and these didn't work for me:
returning shouldInvalidateLayoutForBoundsChange to true as suggested here
subclassing UICollectionViewFlowLayout as suggested here
If anyone out there decides to attempt the solution for the same case in a splitViewController can take note.
I want to implement UITableView's UISwipeActionsConfiguration for a UICollectionView. In order to do so, I am using SwipeCellKit - github
My UICollectionView adopts to the SwipeCollectionViewCellDelegate protocol. And the cell inherits from SwipeCollectionViewCell.
ViewController with UICollectionView
class SwipeViewController: UIViewController {
lazy var collectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .vertical
let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
collectionView.register(SwipeableCollectionViewCell.self, forCellWithReuseIdentifier: SwipeableCollectionViewCell.identifier)
collectionView.showsVerticalScrollIndicator = false
collectionView.contentInset = UIEdgeInsets(top: 8, left: 0, bottom: 4, right: 0)
collectionView.backgroundColor = UIColor(white: 0.97, alpha: 1)
collectionView.dataSource = self
collectionView.delegate = self
return collectionView
}()
var items: [String] = {
var items = [String]()
for i in 1 ..< 20 {
items.append("Item \(i)")
}
return items
}()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(collectionView)
collectionView.setConstraints(topAnchor: view.topAnchor,
leadingAnchor: view.leadingAnchor,
bottomAnchor: view.bottomAnchor,
trailingAnchor: view.trailingAnchor,
leadingConstant: 10,
trailingConstant: 10)
}
}
extension SwipeViewController: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: collectionView.frame.width, height: 80)
}
}
extension SwipeViewController: UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return items.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: SwipeableCollectionViewCell.identifier, for: indexPath) as! SwipeableCollectionViewCell
cell.backgroundColor = BackgroundColor.colors[indexPath.row]
cell.delegate = self
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
}
}
extension SwipeViewController: SwipeCollectionViewCellDelegate {
func collectionView(_ collectionView: UICollectionView, editActionsForItemAt indexPath: IndexPath, for orientation: SwipeActionsOrientation) -> [SwipeAction]? {
guard orientation == .right else { return nil }
let deleteAction = SwipeAction(style: .destructive, title: "Delete") { action, indexPath in
}
return [deleteAction]
}
}
SwipeCollectionViewCell
class SwipeableCollectionViewCell: SwipeCollectionViewCell {
override init(frame: CGRect) {
super.init(frame: frame)
self.addSubview(nameLabel)
nameLabel.setConstraints(topAnchor: self.topAnchor,
leadingAnchor: self.leadingAnchor,
bottomAnchor: self.bottomAnchor,
trailingAnchor: self.trailingAnchor)
self.backgroundColor = .white
}
static let identifier = "TaskListTableViewCell"
private let nameLabel: UILabel = {
let label = UILabel()
label.text = "Simulator user has requested new graphics quality"
label.numberOfLines = 0
return label
}()
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
After doing so, when I swipe the cell, the deleteAction overlaps with the content of the cell.
As you see in the screenshot, the cell's content overlaps with the deleteAction text.
Update
setConstraints sets views's translatesAutoresizingMaskIntoConstraints to false
You must add nameLabel to contentView.
Change
self.addSubview(nameLabel)
nameLabel.setConstraints(topAnchor: self.topAnchor,
leadingAnchor: self.leadingAnchor,
bottomAnchor: self.bottomAnchor,
trailingAnchor: self.trailingAnchor)
to
self.contentView.addSubview(nameLabel)
nameLabel.setConstraints(topAnchor: self.contentView.topAnchor,
leadingAnchor: self.contentView.leadingAnchor,
bottomAnchor: self.contentView.bottomAnchor,
trailingAnchor: self.contentView.trailingAnchor)
Result:
I am have added collection view to two of my view controllers and when I click on the cell in the first view controller (TabBar.swift) it should go to the second view controller and vice Versa, but for some reason, the app crashes when I launch.
Here is the code for the first view controller: TabBar.swift
struct OpenViewController {
var viewController: UIViewController
}
class TabBar: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
addCollectionViewConstraints()
}
var viewControllerList = [
OpenViewController(viewController: FirstScreen())
]
// Setup the collection veiw
let collectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
cv.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "cellID")
cv.translatesAutoresizingMaskIntoConstraints = false
return cv
}()
// Add constraints to the collection view
func addCollectionViewConstraints() {
view.addSubview(collectionView)
collectionView.backgroundColor = .blue
collectionView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 10).isActive = true
collectionView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
collectionView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
collectionView.heightAnchor.constraint(equalTo: collectionView.widthAnchor, multiplier: 0.3).isActive = true
collectionView.delegate = self
collectionView.dataSource = self
}
}
Here is the code for the second view controller:
FirstScreen.swift
struct OpenViewControllerTwo {
var viewController: UIViewController
}
class FirstScreen: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .yellow
addCollectionViewConstraints()
}
var viewControllerList = [
OpenViewControllerTwo(viewController: FirstScreen())
]
let collectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
cv.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "cellID")
cv.translatesAutoresizingMaskIntoConstraints = false
return cv
}()
// Add constraints to the collection view
func addCollectionViewConstraints() {
view.addSubview(collectionView)
collectionView.backgroundColor = .blue
collectionView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 10).isActive = true
collectionView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
collectionView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
collectionView.heightAnchor.constraint(equalTo: collectionView.widthAnchor, multiplier: 0.3).isActive = true
collectionView.delegate = self
collectionView.dataSource = self
}
}
extension FirstScreen: UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {
// The number of elements inside the collection view
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 1
}
// Adding the cells
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellID", for: indexPath)
// Customize the cells
cell.backgroundColor = .black
cell.layer.cornerRadius = collectionView.contentSize.height / 2
cell.layer.borderWidth = 1
cell.layer.borderColor = UIColor.black.cgColor
return cell
}
// The witdth and the height of the cell
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: collectionView.frame.height, height: collectionView.frame.height)
}
// Padding to the cells
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsets(top: 0, left: 8, bottom: 0, right: 8)
}
// Selecting a cell and navigating to another view controller
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let openViewController = self.viewControllerList[indexPath.row]
self.present(openViewController.viewController, animated: false)
}
}
Crash log:
Your are getting memory issue because of viewControllerList is initialised in loops.
Just declare variable like this
var viewControllerList = [OpenViewController]()
then assign value like this in viewDidLoad.
viewControllerList = [
OpenViewController(viewController: FirstScreen())
]
I found that code in FirstScreen
var viewControllerList = [
OpenViewControllerTwo(viewController: FirstScreen())
]
When FirstScreen init, viewControllerList will init all call FirstScreen.
It created recursive recursive. Please change above code, put another class, ex: In FirstClass
var viewControllerList = [
OpenViewControllerTwo(viewController: SecondClass())
]
Basically, I have a UICollectionView within a UIView (that extends the collection view data and delegate protocols). Certain UICollectionView functions, such as numberofSections and numberOfItemsInSection are being called, while others such as cellForRowAt are not being called.
The collection view in question is rolesCollectionView, and its data is the roles array. I have added some print statements to see which functions are being called, and only the aforementioned ones are. I'm also certain that this isn't an issue with the RolesCollectionViewCell, whose code I can also provide if necessary.
class ProjectSearchViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
var rolesCollectionView: UICollectionView!
var roles = ["Developer", "Designer", "Backend Developer", "Frontend Developer"]
override func viewDidLoad() {
super.viewDidLoad()
print("test")
// Do any additional setup after loading the view.
navigationItem.title = "Search Projects"
view.backgroundColor = .white
var rolesLayout = UICollectionViewFlowLayout()
rolesLayout.scrollDirection = .horizontal
rolesLayout.minimumLineSpacing = 8
rolesLayout.minimumInteritemSpacing = 8
rolesLayout.sectionInset = UIEdgeInsets(top: 0, left: 8, bottom: 0, right: 8)
rolesLayout.estimatedItemSize = CGSize(width: 50, height: 25)
rolesCollectionView = UICollectionView(frame: .zero, collectionViewLayout: rolesLayout)
rolesCollectionView.backgroundColor = .white
rolesCollectionView.delegate = self
rolesCollectionView.dataSource = self
rolesCollectionView.register(RolesCollectionViewCell.self, forCellWithReuseIdentifier: "role")
rolesCollectionView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(rolesCollectionView)
rolesCollectionView.reloadData()
let newView = UIView()
newView.backgroundColor = .black
newView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(newView)
NSLayoutConstraint.activate([
newView.widthAnchor.constraint(equalToConstant: 50),
newView.heightAnchor.constraint(equalToConstant: 50),
newView.centerXAnchor.constraint(equalTo: view.centerXAnchor),
newView.centerYAnchor.constraint(equalTo: view.centerYAnchor)
])
setupConstraints()
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
// if collectionView == self.rolesCollectionView {
print("return")
print(roles.count)
return roles.count
// } else {
// return 0
// }
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
print("section")
return 1
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
print("cell for item at")
if collectionView == self.rolesCollectionView {
var cell = rolesCollectionView.dequeueReusableCell(withReuseIdentifier: "role", for: indexPath) as! RolesCollectionViewCell
cell.configure(roleName: roles[indexPath.row])
print(roles[indexPath.row])
return cell
} else {
return UICollectionViewCell()
}
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
//if collectionView == self.rolesCollectionView {
print("size")
print("test: \(collectionView == rolesCollectionView)")
return CGSize(width: 15.0, height: 15.0)
// }
}
func setupConstraints() {
NSLayoutConstraint.activate([
rolesCollectionView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 8),
rolesCollectionView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -8),
rolesCollectionView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 8)
//rolesCollectionView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bot)
])
}
}
I am attempting to get a UICollectionView to be inside a UIView as the framework I'm using requires a UIView to be returned. I have looked at this question: How do I add a UICollectionView to a UIView as a subview? and Adding UICollectionView inside UIView without Storyboards but not sure how to get it to work.
I have a attempted it like this:
class TopView : UIView, UICollectionViewDataSource, UICollectionViewDelegate {
override init(frame: CGRect) {
super.init(frame: frame)
self.backgroundColor = .red
addSubview(collectionView)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
lazy var collectionView : UICollectionView = {
let layout = UICollectionViewFlowLayout()
let cv = UICollectionView(frame: CGRect.zero, collectionViewLayout: layout)
cv.translatesAutoresizingMaskIntoConstraints = false
cv.delegate = self
cv.dataSource = self
cv.register(HeaderCell.self, forCellWithReuseIdentifier: "HeaderCell")
cv.backgroundColor = .yellow
return cv
}()
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "HeaderCell", for: indexPath) as! HeaderCell
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: self.collectionView.frame.width, height: 200)
}
func collectionView(_ collectionView: UICollectionView, numberOfSections section: Int) -> Int {
return 1
}
}
But I'm getting a blank screen.
Update:
This is how I add the TopView to a UIViewController:
class MainViewController: UIViewController {
var mainView = TopView()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(mainView)
}
}
I get just a black screen.
TopView.swift
class TopView : UIView, UICollectionViewDataSource, UICollectionViewDelegate {
lazy var collectionView : UICollectionView = {
let layout = UICollectionViewFlowLayout()
let cv = UICollectionView(frame: CGRect.zero, collectionViewLayout: layout)
//If you set it false, you have to add constraints.
cv.translatesAutoresizingMaskIntoConstraints = false
cv.delegate = self
cv.dataSource = self
cv.register(HeaderCell.self, forCellWithReuseIdentifier: "HeaderCell")
cv.backgroundColor = .yellow
return cv
}()
override init(frame: CGRect) {
super.init(frame: frame)
self.backgroundColor = .red
addSubview(collectionView)
//Add constraint
collectionView.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
collectionView.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true
collectionView.topAnchor.constraint(equalTo: topAnchor).isActive = true
collectionView.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
func collectionView(_ collectionView: UICollectionView, numberOfSections section: Int) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 30
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "HeaderCell", for: indexPath) as! HeaderCell
cell.backgroundColor = .cyan
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: self.collectionView.frame.size.width, height: 200)
}
}
ViewController.swift
lazy var topView: TopView = {
let tv = TopView()
tv.translatesAutoresizingMaskIntoConstraints = false
return tv
}()
func addTopView() {
view.addSubview(topView)
topView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
topView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
topView.topAnchor.constraint(equalTo: view.topAnchor, constant: 300).isActive = true
topView.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
topView.heightAnchor.constraint(equalToConstant: 200).isActive = true
}
Call addTopView() from viewDidLoad()