Keep Collection View Cells Overlapped When They Are Redrawn - ios

I've gotten my cells to overlap by setting the minimumLineSpacing property of the collection views layout to negative. But, when I scroll and the cells are redrawn, they now overlap in the opposite direction. I've put pictures below.
How do I keep the cells overlapping as seen in the first picture when the collection view is scrolled and cells are redraw?
import UIKit
class PopularView: UIView {
let cellID = "cellID"
// MARK: - Views
let collectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .vertical
layout.minimumLineSpacing = -55 // -------Allows Overlap-----
layout.itemSize = CGSize(width: SCREEN_WIDTH, height: 185)
layout.minimumInteritemSpacing = 17
let view = UICollectionView(frame: CGRect.zero, collectionViewLayout: layout)
view.backgroundColor = .white
return view
}()
// MARK: - Initializers
override init(frame: CGRect) {
super.init(frame: frame)
collectionView.dataSource = self
collectionView.register(PopularCell.self, forCellWithReuseIdentifier: cellID)
backgroundColor = .white
setupCollectionView()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
// MARK: - Setup
fileprivate func setupCollectionView() {
self.addSubview(collectionView)
collectionView.anchors(top: self.topAnchor, topPad: 0, bottom: self.bottomAnchor, bottomPad: 0, left: self.leftAnchor, leftPad: 0, right: self.rightAnchor, rightPad: 0, height: nil, width: nil)
collectionView.contentSize = CGSize(width: 700, height: 700)
}
}
extension PopularView: UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 500
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellID, for: indexPath) as! PopularCell
cell.background.backgroundColor = .random
return cell
}
}

Try this, that might help you:
1- Inside your Cells, you could define innerView inside your cell and set the frame to
let innerView:UIView = CGRect(x: 0,y: -overlapHeight,width: screenWidth, height:cell.height + overlapHeight)
cell?.contentView.addSubview(innerView)
2- Configure your cell during initialisation with this:
cell?.contentView.clipsToBounds = false
3- When loading cell, set the z-order:
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath)
cell.layer.zPosition = CGFloat(indexPath.row)
// configure your cell after here
}
You should be able to see the nested views inside your content view to have overlapping.
I have drafted a sample code, not looking perfectly, but will help you get started:
private let reuseIdentifier = "Cell"
private let overlapHeight:CGFloat = 100
class CustomCollectionCell:UICollectionViewCell {
var innerView:UIView?
override init(frame: CGRect) {
super.init(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 300))
self.backgroundColor = .darkGray
let innerView = UIView(frame: CGRect(x: 0,y: -overlapHeight,width: UIScreen.main.bounds.width,height: overlapHeight + self.contentView.frame.height))
self.innerView = innerView
innerView.layer.cornerRadius = 20
self.contentView.addSubview(innerView)
self.contentView.clipsToBounds = false
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func configure(color:UIColor?) {
innerView?.backgroundColor = color
}
}
import UIKit
private let reuseIdentifier = "Cell"
private let overlapHeight:CGFloat = 100
class CustomCollectionCell:UICollectionViewCell {
var innerView:UIView?
override init(frame: CGRect) {
super.init(frame: frame)
self.backgroundColor = .darkGray
let innerView = UIView(frame: CGRect(x: 0,y: -overlapHeight,width: UIScreen.main.bounds.width,height: overlapHeight + self.contentView.frame.height))
self.innerView = innerView
innerView.layer.cornerRadius = 20
self.contentView.addSubview(innerView)
self.contentView.clipsToBounds = false
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func configure(color:UIColor?) {
innerView?.backgroundColor = color
}
}
class CollectionViewController: UICollectionViewController {
override func viewDidLoad() {
super.viewDidLoad()
let flowLayout = UICollectionViewFlowLayout()
flowLayout.itemSize = CGSize(width: UIScreen.main.bounds.width, height: 190)
flowLayout.sectionInset = UIEdgeInsets(top: 0, left: 5, bottom: 0, right: 5)
flowLayout.scrollDirection = .vertical
flowLayout.minimumInteritemSpacing = 0.0
collectionView.collectionViewLayout = flowLayout
// Register cell classes
self.collectionView!.register(CustomCollectionCell.self, forCellWithReuseIdentifier: reuseIdentifier)
}
// MARK: UICollectionViewDataSource
override func numberOfSections(in collectionView: UICollectionView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of items
return 30
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath)
cell.layer.zPosition = CGFloat(indexPath.row)
var color:UIColor?
switch indexPath.row % 4 {
case 0:
color = .purple
case 1:
color = .yellow
case 2:
color = .green
default:
color = .red
}
if let cell = cell as? CustomCollectionCell {
cell.configure(color: color)
}
return cell
}
}
Result:

Related

Can't reload background image when dismissing

I need to reload a background image in the MainVC when the user chooses it from the ViewController that will be dismissed but I can't figure out how. Any idea?
Ps. In the MainVC I have a collectionView, every cell contain another collectionView and these collectionViews have a custom cell.
When the user chooses a background image that image is passed to the custom cell and should be set as background.
Protocol
protocol ThemeDelegate {
func handlePassThemeData(data: UIImage)
}
MainVC
class MainVC: UIViewController, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource, ThemeDelegate {
var currentTheme = UIImage(named: "4")!
let collectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .vertical
let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
collectionView.translatesAutoresizingMaskIntoConstraints = false
collectionView.register(QuoteCell.self, forCellWithReuseIdentifier: reuseIdentifier)
collectionView.register(FeaturedCell.self, forCellWithReuseIdentifier: featuredReuseIdentifier)
collectionView.register(AuthorCell.self, forCellWithReuseIdentifier: authorReuseIdentifier)
return collectionView
}()
override func viewDidLoad() {
super.viewDidLoad
setUpViews()
setupNavigationBarItems()
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if indexPath.item == 0 {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! QuoteCell
createGradientLayer(bounds: CGRect(x: 0, y: 0, width: cell.frame.width, height: cell.frame.height), cell: cell)
cell.delegate = self
cell.imageToPass = currentTheme
return cell
} else if indexPath.item == 1 {
...
}
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
if indexPath.item == 0 {
return CGSize(width: view.frame.width, height: view.frame.height / 1.5)
} else if indexPath.item == 1 {
return CGSize(width: view.frame.width, height: view.frame.height / 1.95)
} else {
return CGSize(width: view.frame.width, height: 160)
}
}
func setupNavigationBarItems() {
let themesButton = setUpBarButton(image: #imageLiteral(resourceName: "themes_icon_color"))
themesButton.addTarget(self, action: #selector(handleThemesTapped), for: .touchUpInside)
navigationItem.leftBarButtonItem = UIBarButtonItem(customView: themesButton)
}
func setUpViews() {
view.addSubview(collectionView)
collectionView.delegate = self
collectionView.dataSource = self
collectionView.anchors(top: view.safeAreaLayoutGuide.topAnchor, left: view.leftAnchor, bottom: view.bottomAnchor, right: view.rightAnchor)
}
func handlePassThemeData(data: UIImage) {
self.currentTheme = data
self.collectionView.reloadData()
}
#objc func handleThemesTapped() {
let themesVc = ThemesVC(collectionViewLayout: UICollectionViewFlowLayout())
let navController = UINavigationController(rootViewController: themesVc)
themesVc.delegate = self
navigationController?.present(navController, animated: true, completion: nil)
}
}
MainVC Cell with ColletionView
class QuoteCell: UICollectionViewCell, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {
var imageToPass = UIImage(named: "2")!
let collectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .horizontal
let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
collectionView.translatesAutoresizingMaskIntoConstraints = false
collectionView.register(QuoteSubCell.self, forCellWithReuseIdentifier: "cell")
return collectionView
}()
override init(frame: CGRect) {
super.init(frame: frame)
setUpViews()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! QuoteSubCell
cell.quote = quotes[indexPath.item]
cell.setupCell(with: imageToPass)
return cell
}
func setUpViews() {
addSubview(collectionView)
collectionView.delegate = self
collectionView.dataSource = self
collectionView.anchors(top: nameLabel.bottomAnchor, left: leftAnchor, bottom: bottomAnchor, right: rightAnchor)
}
}
CustomCell
class QuoteSubCell: UICollectionViewCell {
var backgroundImage: UIImageView = {
let view = UIImageView()
view.clipsToBounds = true
view.contentMode = .scaleAspectFill
view.image = UIImage(named: "2")
return view
}()
override init(frame: CGRect) {
super.init(frame: frame)
setUpViews()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func setupCell(with image: UIImage) {
backgroundImage.image = image
}
func setUpViews() {
contentView.addSubview(backgroundImage)
backgroundImage.anchors(top: contentView.topAnchor, left: contentView.leftAnchor, bottom: contentView.bottomAnchor, right: contentView.rightAnchor)
}
}
ThemesVC where the user chooses the background
class ThemesVC: UICollectionViewController, UICollectionViewDelegateFlowLayout {
var delegate: ThemeDelegate?
override func viewDidLoad() {
super.viewDidLoad()
collectionView.dataSource = self
collectionView.delegate = self
setUpViews()
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! ThemeCell
cell.backgroundImage.image = themeCell.backgroundImages[indexPath.item]
cell.layer.cornerRadius = 10
return cell
}
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
delegate?.handlePassThemeData(data: themeCell.backgroundImages[indexPath.item])
self.navigationController?.dismiss(animated: true, completion: nil)
}
func setUpViews() {
collectionView.register(ThemeCell.self, forCellWithReuseIdentifier: reuseIdentifier)
collectionView.backgroundColor = UIColor(white: whitePoint, alpha: 1)
collectionView.alwaysBounceVertical = true
collectionView.showsVerticalScrollIndicator = false
}
}
You almost got it right.
class MainVC: UIViewController, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource, ThemeDelegate {
//MARK: Properties
var currentTheme = UIImage(named: "defaultImage")
override func viewDidLoad() {
super.viewDidLoad()
setUpViews()
}
func setUpViews() {
view.addSubview(collectionView)
collectionView.anchors(top: view.safeAreaLayoutGuide.topAnchor, left: view.leftAnchor, bottom: view.bottomAnchor, right: view.rightAnchor)
}
func handlePassThemeData(data: UIImage) {
self.currentTheme = data //override your variable with the new image
self.collectionView.reloadData() //reload the collectionView, to apply the change
}
#objc func handleThemesTapped() {
let themesVc = ThemesVC(collectionViewLayout: UICollectionViewFlowLayout())
themesVc.delegate = self
//no need to instantiate a new navigationController. Just push to the current one, if you want the animation from right, and not from bottom-up.
navigationController?.pushViewController(themesVc, animated: true)
}
}
Delegates should always be weak referenced.
class ThemesVC: UICollectionViewController, UICollectionViewDelegateFlowLayout {
weak var delegate: ThemeDelegate?
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
delegate?.handlePassThemeData(data: themeCell.backgroundImages[indexPath.item])
self.navigationController?.popViewController(animated: true)
}
}
Also, you don't need to create a static variable inside the cell. Instead, pass your currentImage to your cell's variable imageToPass in the method cellForItemAt.
class QuoteCell: UICollectionViewCell, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {
var imageToPass = UIImage(named: "defaultImage")//pass this image to the cells inside the collectionView
override init(frame: CGRect) {
super.init(frame: frame)
setUpViews()
}
func setUpViews() {
addSubview(collectionView)
collectionView.anchors(top: nameLabel.bottomAnchor, left: leftAnchor, bottom: bottomAnchor, right: rightAnchor)
}
}
Finally, your subCells should show your image:
class QuoteSubCell: UICollectionViewCell {
var backgroundImage: UIImageView = {
let view = UIImageView()
view.clipsToBounds = true
view.contentMode = .scaleAspectFill
view.image = QuoteSubCell.chosenTheme
return view
}()
override init(frame: CGRect) {
super.init(frame: frame)
setUpViews()
}
private func setUpViews() {
contentView.addSubview(backgroundImage)
backgroundImage.anchors(top: contentView.topAnchor, left: contentView.leftAnchor, bottom: contentView.bottomAnchor, right: contentView.rightAnchor)
}
func setupCell(with image: UIImage) { //use this setup function inside "cellForItemAt" method, where you will pass the `imageToPass` image.
backgroundImage.image = image
}
}
UPDATE:
cellForItemAt inside MainVC:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if indexPath.item == 0 {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! QuoteCell
createGradientLayer(bounds: CGRect(x: 0, y: 0, width: cell.frame.width, height: cell.frame.height), cell: cell)
cell.delegate = self
cell.imageToPass = currentTheme //here you pass the image the 1st time
return cell
} else if indexPath.item == 1 {
...
}
}
cellForItemAt inside QuoteCell:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! QuoteSubCell
cell.quote = quotes[indexPath.item]
cell.setupCell(with: imageToPass) //here you pass the image the second time
return cell
}
If you're still unable to make it work, I will ask you to share the code from the cellForItemAt methods from the both collectionViews.

How can use UIView on top of collectionView, so that when i scroll cells, UIView should not move?

I am creating horizontal carousel using collectionView Cells and I want to apply one custom view which has label on it on top of the collectionView, so that when i scroll cells the UIView should remain but changing the text of the label and cells should change. Please help me..
Hope This is What you are expecting.
Did everything programmatically for demo purpose. this is how CollectionViewController and the whole code looks like.
//
// SliderController.swift
// AssignmentSO
//
// Created by Chanaka Caldera on 7/5/19.
// Copyright © 2019 StackOverflow. All rights reserved.
//
import UIKit
private let reuseIdentifier = "Cell"
class SliderController: UICollectionViewController {
var topview: UIView!
var label: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
//MARK: - set up top view and label
topview = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 100))
topview.backgroundColor = .green
view.addSubview(topview)
label = UILabel(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 100))
label.text = "Title 0.0"
label.textAlignment = .center
label.font = UIFont.systemFont(ofSize: 40)
topview.addSubview(label)
// MARK: - Registering the cell and set EdgeInsets (if you are using storyboard set EdgeInset would be enough
self.collectionView!.register(CustomCell.self, forCellWithReuseIdentifier: reuseIdentifier)
self.collectionView.contentInset = UIEdgeInsets(top: 100, left: 0, bottom: 0, right: 0)
self.collectionView.isPagingEnabled = true
}
}
// MARK: Datasource
extension SliderController {
override func numberOfSections(in collectionView: UICollectionView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of items
return 5
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! CustomCell
cell.label.text = "Cell \(indexPath.row)"
return cell
}
}
extension SliderController: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let height = UIScreen.main.bounds.height - 100
let width = UIScreen.main.bounds.width
return CGSize(width: width, height: height)
}
}
extension SliderController {
override func scrollViewDidScroll(_ scrollView: UIScrollView) {
let xvalue = scrollView.contentOffset.x
let width = UIScreen.main.bounds.width
let cellIndex = xvalue/width
switch cellIndex {
case 0:
label.text = "Title \(cellIndex)"
case 1:
label.text = "Title \(cellIndex)"
case 2:
label.text = "Title \(cellIndex)"
case 3:
label.text = "Title \(cellIndex)"
case 4:
label.text = "Title \(cellIndex)"
default:
label.text = ""
}
}
}
//MARK: Custom cell
class CustomCell: UICollectionViewCell {
var label: UILabel!
override init(frame: CGRect) {
super.init(frame: frame)
backgroundColor = .lightGray
label = UILabel(frame: CGRect(x: 0, y: 0, width: contentView.bounds.size.width, height: contentView.bounds.size.height))
label.font = UIFont.systemFont(ofSize: 40)
label.text = "2"
label.textAlignment = .center
addSubview(label)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
hope this will help to someone and did every thing on same file for demo purpose. sorry for that. cheers!
You can load UIView() in collectionView section header like below.
Create Header Xib with class type UICollectionReusableView
Register Xib that you just created
collectionFeatured.register(TodaySectionHeader.nib, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "TodaySectionHeader")
CollectionView Delegate and DataSource
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
return CGSize(width: collectionView.bounds.width, height: 80)
}
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
let sectionHeader = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "TodaySectionHeader", for: indexPath) as! TodaySectionHeader
sectionHeader.labelDate.text = Date().toString(format: "EEEE dd MMMM").uppercased()
sectionHeader.labelTitle.text = "Today"
return sectionHeader
}
If you want to stick that header on top now you have to set UICollectionViewFlowLayout during collection initialization where you set delegate datasource and register xib basically in viewDidLoad
let layout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout // casting is required because UICollectionViewLayout doesn't offer header pin. Its feature of UICollectionViewFlowLayout
layout?.sectionHeadersPinToVisibleBounds = true

MyUICollectionViewCell is not visible

import UIKit
private let reuseIdentifier = "NewCell"
class SeondViewController: UIViewController, UICollectionViewDelegateFlowLayout {
#IBOutlet var myNavBar: UINavigationBar!
var frutta: [String]!
override func viewDidLoad() {
super.viewDidLoad()
let margins = self.view.layoutMarginsGuide
let layout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 30, left: 10, bottom: 10, right: 10)
layout.scrollDirection = .vertical
let newCollectionView: UICollectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
newCollectionView.dataSource = self
newCollectionView.delegate = self
newCollectionView.register(SecondTabCollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
newCollectionView.reloadData()
newCollectionView.backgroundColor = UIColor.clear
newCollectionView.isHidden = false
newCollectionView.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(newCollectionView)
frutta = ["Mele", "Olive", "Pere", "Noci", "Banane", "Kiwi", "Ananas"]
myNavBar.leadingAnchor.constraint(equalTo: margins.leadingAnchor).isActive = true
myNavBar.topAnchor.constraint(equalTo: margins.topAnchor).isActive = true
myNavBar.trailingAnchor.constraint(equalTo: margins.trailingAnchor).isActive = true
newCollectionView.leadingAnchor.constraint(equalTo: margins.leadingAnchor).isActive = true
newCollectionView.topAnchor.constraint(equalTo: myNavBar.bottomAnchor).isActive = true
newCollectionView.trailingAnchor.constraint(equalTo: margins.trailingAnchor).isActive = true
◦ newCollectionView.bottomAnchor.constraint(equalTo: margins.bottomAnchor).isActive = true
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: collectionView.bounds.size.width / 2, height: collectionView.bounds.size.height / 3)
}
}
extension SecondViewController: UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return frutta.count
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let myCell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! SecondTabCollectionViewCell
myCell.backgroundColor = UIColor.clear
myCell.secondCellLabel = UILabel()
myCell.secondCellLabel.frame.size = CGSize(width: myCell.bounds.width / 2, height: myCell.secondCellLabel.bounds.height / 2)
myCell.secondCellLabel.adjustsFontSizeToFitWidth = true
myCell.secondCellLabel.backgroundColor = UIColor.clear
myCell.secondCellLabel.textColor = UIColor.black
myCell.secondCellLabel.text = frutta[indexPath.item]
myCell.translatesAutoresizingMaskIntoConstraints = false
myCell.contentView.addSubview(myCell.secondCellLabel)
return myCell
}
}
extension SecondViewController: UICollectionViewDelegate {
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
print("Hai premuto \(indexPath.row).")
}
}
import UIKit
class SecondTabCollectionViewCell: UICollectionViewCell {
var secondCellLabel: UILabel!
override init(frame: CGRect) {
super.init(frame: frame)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) non e stato implementato.")
}
}
myCell.secondCellLabel.frame.size = CGSize(width: myCell.bounds.width / 2, height: myCell.secondCellLabel.bounds.height / 2)
The above line is wrong in cellForItemAt, you put the height of label is myCell.secondCellLabel.bounds.height instead of myCell.bounds.height.
correct one is
myCell.secondCellLabel.frame.size = CGSize(width: myCell.bounds.width / 2, height: myCell.bounds.height / 2)
It is a little strange to be laying out a cell's internal views in cellForRowAtIndexPath, this should be done in the cell class itself. The basic problem is that you your UILabel is appearing in the top left corner of the cell and has a height of zero, so you cannot see it. I moved the code you had in cellForRowAtIndexPath into the cell itself.
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let myCell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! SecondTabCollectionViewCell
myCell.backgroundColor = UIColor.clear
myCell.secondCellLabel.text = frutta[indexPath.item]
return myCell
}
import UIKit
class SecondTabCollectionViewCell: UICollectionViewCell {
var secondCellLabel: UILabel = {
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.backgroundColor = .clear
label.textColor = .black
label.adjustsFontSizeToFitWidth = true
label.textAlignment = .center
return label
}()
override init(frame: CGRect) {
super.init(frame: frame)
configureViews()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
private func configureViews() {
addSubview(secondCellLabel)
}
override func layoutSubviews() {
super.layoutSubviews()
configureLayout()
}
private func configureLayout() {
let width = bounds.width / 2
let height = frame.height / 2
let size = CGSize(width: width, height: height)
let origin = CGPoint(x: width/2, y: height/2)
secondCellLabel.frame = CGRect(origin: origin, size: size)
}
}

Swift how to resize UICollectionViewCell based on it's content

I have got this Swift code
let collectionView = UICollectionView(frame: CGRect(x:0,y:0,width:0,height:0),
collectionViewLayout: UICollectionViewFlowLayout())
collectionView.translatesAutoresizingMaskIntoConstraints = false
collectionView.register(PostCell.self, forCellWithReuseIdentifier: cellId)
collectionView.delegate = self
collectionView.dataSource = self
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! PostCell
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets
{
return UIEdgeInsets(top: 15, left: 5, bottom: 15, right: 5)
}
My goal is to make cell's height automatic based on it's content.I wanted to implement
if let flowLayout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout {
flowLayout.estimatedItemSize = UICollectionViewFlowLayoutAutomaticSize
}
and
let layoutHeight = UICollectionViewFlowLayout()
layoutHeight.estimatedItemSize = CGSize(width: 100, height: 100)
collectionView = UICollectionView(frame: CGRect(x:0,y:0,width:0,height:0), collectionViewLayout: layoutHeight)
But the first one made all cells 50x50 and the second one made them 100x100.Here is my PostCell class
class PostCell: UICollectionViewCell {
override init(frame: CGRect) {
super.init(frame: frame)
designCell()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
let CellprofileImage: UIImageView = {
let v = UIImageView()
v.layer.cornerRadius = 55
v.layer.masksToBounds = true
v.layer.borderWidth = 3
v.layer.borderColor = UIColor(white: 0.5, alpha: 1).cgColor
v.image = #imageLiteral(resourceName: "guitar")
v.translatesAutoresizingMaskIntoConstraints = false
return v
}()
func designCell(){
addSubview(CellprofileImage)
backgroundColor = .red
cellConstraints()
}
func cellConstraints(){
CellprofileImage.topAnchor.constraint(equalTo: self.topAnchor,constant:20).isActive = true
CellprofileImage.leftAnchor.constraint(equalTo: self.leftAnchor,constant:20).isActive = true
CellprofileImage.widthAnchor.constraint(equalToConstant: 310).isActive = true
CellprofileImage.heightAnchor.constraint(equalToConstant: 310).isActive = true
}
}

Change Color and text of items in cell in collectionView

I want change color of circleView and text in label in cell in CollectionView using function.
Class of my Cell:
class CustomCell: UICollectionViewCell {
override init(frame: CGRect) {
super.init(frame: frame)
setupViews()
}
var timerLabel:UILabel = {
let label = UILabel()
label.text = "5"
label.frame = CGRect(x: 200, y: 10, width: 60, height: 60)
label.backgroundColor = .white
label.textAlignment = NSTextAlignment.center
return label
}()
var circleView: UIView = {
let circleView = UIView()
circleView.frame = CGRect(x: 100, y: 10, width: 60, height: 60)
circleView.backgroundColor = .red
circleView.layer.cornerRadius = 30
circleView.layer.masksToBounds = true
return circleView
}()
func setupViews() {
backgroundColor = .white
addSubview(timerLabel)
addSubview(circleView)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setupViews()
}
}
Create CollectionView in VC:
func collectionViewSet() {
collectionView?.frame.size.height = (view.frame.height / 100) * 70
collectionView?.backgroundColor = .white
collectionView?.register(CustomCell.self, forCellWithReuseIdentifier: cellId)
}
//-------------- (CollectionView Configure) -------------------
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 5
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width:view.frame.width ,height: collectionView.frame.height / 6)
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId , for: indexPath)
customCell.tag = indexPath.row + 1
return cell
}
And in my created func for example setColor() i want generate ranodm color (by UIColor) for CircleView and generate random number for TimerLable but only in one cell. For next cell I want generate another color and number. Is there any way to do that, i was trying use indexPath.row and func didselectedItemForindexPath but it didn't works at all. Maybe I did something wrong.
Ok I have found an answer. I post my code if someone will have similar problem :)
for x in 0...4{
let indexPath = IndexPath(row: x, section: 0)
guard let cell = collectionView?.cellForItem(at: indexPath) as? CustomCell else {
return
}
cell.circleView.backgroundColor = .blue
}
There are many, many examples of generating random colors and numbers out there - a quick search and you'll have plenty to choose from.
Once you've picked a couple, change your cellForItemAt function to:
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId , for: indexPath)
customCell.tag = indexPath.row + 1
let randColor = myRandomColorFunc()
let randNumber = myRandomNumberFunc()
customCell.circleView.backgroundColor = randColor
customCell.timerLabel.text = "\(randNumber)"
return cell
}

Resources