How do I clear images from my UIImageView - swift

I have the following code for a compositionalLayout in Swift, but my images are not going away with the reuse of cells.
import UIKit
class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
#IBOutlet var collectionView: UICollectionView!
let myInset: CGFloat = 4.0
let dataColors = [UIColor.red, UIColor.blue, UIColor.green, UIColor.magenta, UIColor.purple, UIColor.orange, UIColor.red, UIColor.blue, UIColor.green, UIColor.magenta, UIColor.purple, UIColor.systemYellow, UIColor.red, UIColor.blue, UIColor.green, UIColor.magenta, UIColor.purple, UIColor.orange, UIColor.red, UIColor.blue, UIColor.green, UIColor.magenta, UIColor.purple, UIColor.systemYellow]
let theImages = [
"MEN_8882","002","003","004","005","006","001","002","003","004","005","006",
"MEN_8882","002","003","004","005","006","001","002","003","004","005","006"
]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
collectionView.setCollectionViewLayout(createCustomLayout(), animated: false)
collectionView.backgroundColor = .white
self.collectionView.delegate = self
self.collectionView.dataSource = self
collectionView.register(QuickCell.self, forCellWithReuseIdentifier: "cellID")
//configureCollectionView()
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1//dataColors.count
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return dataColors.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellID", for: indexPath) as? QuickCell {
cell.backgroundColor = dataColors[indexPath.row]
let mySubView = UIImageView()
mySubView.image = UIImage(named: theImages[indexPath.row])
cell.addSubview(mySubView)
mySubView.translatesAutoresizingMaskIntoConstraints = false
mySubView.topAnchor.constraint(equalTo: cell.topAnchor, constant: myInset).isActive = true
mySubView.leadingAnchor.constraint(equalTo: cell.leadingAnchor, constant: myInset).isActive = true
mySubView.trailingAnchor.constraint(equalTo: cell.trailingAnchor, constant: myInset * (-1)).isActive = true
mySubView.bottomAnchor.constraint(equalTo: cell.bottomAnchor, constant: myInset * (-1)).isActive = true
mySubView.clipsToBounds = true
// mySubView.layer.cornerRadius = 8
mySubView.contentMode = .scaleAspectFit
cell.clipsToBounds = true
cell.layoutIfNeeded()
//cell.layer.cornerRadius = 12
return cell
} else {
return UICollectionViewCell()
}
}
func createCustomLayout() -> UICollectionViewLayout {
let layout = UICollectionViewCompositionalLayout { (section: Int, environment: NSCollectionLayoutEnvironment) -> NSCollectionLayoutSection? in
let myItemInset: CGFloat = 2.0
let leadingItem = NSCollectionLayoutItem(layoutSize: NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0), heightDimension: .fractionalHeight(1.0)))
leadingItem.contentInsets = NSDirectionalEdgeInsets(top: myItemInset, leading: myItemInset, bottom: myItemInset, trailing: myItemInset)
let leadingGroupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(0.7), heightDimension: .fractionalHeight(1.0))
let leadingGroup = NSCollectionLayoutGroup.vertical(layoutSize: leadingGroupSize, subitem: leadingItem, count: 1)
let trailingGroupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(0.3), heightDimension: .fractionalHeight(1.0))
let trailingGroup = NSCollectionLayoutGroup.vertical(layoutSize: trailingGroupSize, subitem: leadingItem, count: 5)
let fullGroupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0), heightDimension: .fractionalHeight(1.0))
let fullGroup = NSCollectionLayoutGroup.horizontal(layoutSize: fullGroupSize, subitems: [leadingGroup, trailingGroup])
let section = NSCollectionLayoutSection(group: fullGroup)
section.orthogonalScrollingBehavior = .groupPagingCentered
section.contentInsets = NSDirectionalEdgeInsets(top: 20, leading: 0, bottom: 20, trailing: 0)
return section
}
return layout
}
}
The image "MEN..." is portrait while the rest are landscape, and as i scroll back and forth, i see overlapping images in the items.
the code for QuickCell is empty - I'm not sure what to put ther, some kind of initalization? But it should work anyways, right?
import UIKit
class QuickCell: UICollectionViewCell {
}

A subview of type UIImageView is added to your custom Cell (QuickCell) each time your collection view cell is resued. It happens in cellForRowAt delegate method.
So, you have to remove previously added image views from your cell first before adding a new one.
I suggest you move your cell configuration code to QuickCell.
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellID", for: indexPath)
if let quickCell = quickCell as? QuickCell {
quickCell.backgroundColor = self.dataColors[indexPath.row]
quickCell.setImage(self.theImages[indexPath.row], insetBy: self.myInset)
return quickCell
}
return cell
}
Do your custom cell configurations here!
class QuickCell: UICollectionViewCell {
func setImage(_ image: UIImage, insetBy inset: CGFloat) {
// Remove previously added image views first if any
for subview in self.subviews where subview.isKind(of: UIImageView.self) {
subview.removeFromSuperview()
}
let imageView = UIImageView()
imageView.image = image
imageView.clipsToBounds = true
imageView.contentMode = .scaleAspectFit
self.addSubview(imageView)
imageView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
imageView.topAnchor.constraint(equalTo: self.topAnchor, constant: inset),
imageView.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: inset),
imageView.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: inset * (-1)),
imageView.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: inset * (-1))
])
self.layoutIfNeeded()
}
}

To remove image from UIImageView do the following.
yourImageView.image = nil

The problem here is that you are adding a new UIImageView every time the cell is reused. The ones added in previous interations of the cellForItemAtIndexPath: method do not go away, so others are added on top.
These are the problem lines:
let mySubView = UIImageView()
mySubView.image = UIImage(named: theImages[indexPath.row])
cell.addSubview(mySubView)
It would be better to add the image view once when a cell is initialised (or in storyboard with an outlet) then just set the image in the cellForItemAtIndexPath: method.

You keep adding subviews inside cellForItemAt and this causes the overlapping as cells are dequeued , you need to create an outlet for the imageview or create it programmatically inside the cell like
class QuickCell: UICollectionViewCell {
let mySubView = UIImageView()
override init(frame: CGRect) {
super.init(frame: frame)
let myInset: CGFloat = 4.0
self.contentView.addSubview(mySubView)
mySubView.translatesAutoresizingMaskIntoConstraints = false
mySubView.topAnchor.constraint(equalTo: self.contentView.topAnchor, constant: myInset).isActive = true
mySubView.leadingAnchor.constraint(equalTo: self.contentView.leadingAnchor, constant: myInset).isActive = true
mySubView.trailingAnchor.constraint(equalTo: self.contentView.trailingAnchor, constant: myInset * (-1)).isActive = true
mySubView.bottomAnchor.constraint(equalTo: self.contentView.bottomAnchor, constant: myInset * (-1)).isActive = true
mySubView.clipsToBounds = true
// mySubView.layer.cornerRadius = 8
mySubView.contentMode = .scaleAspectFit
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
With
cell.mySubView.image = UIImage(named: theImages[indexPath.row])
or
if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellID", for: indexPath) as? QuickCell {
cell.subviews.forEach {
if $0.tag == 333 {
$0.removeFromSuperview()
}
}
cell.backgroundColor = dataColors[indexPath.row]
let mySubView = UIImageView()
mySubView.tag = 333

This is checked by default for UICollectionView, try to uncheck it. It should work for you.

Related

TextView text alignment not working within collectionViewCell

I’m having trouble with my TexView here And this method textview.textAlignment = .naturalwon’t work I couldn’t figure out what the problem is here the text only stick to the left side no matter what the language is, now this textview is inside a collectionViewCell, I have tried to work around with the NSSting attributes didn’t work tried also to set the text alignment outside of the Cell didn’t work here is my code you will find messageTextView.textAlignment everywhere and all of this places I set it into didn’t work
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "orderReview", for: indexPath) as! OrderReviewCell
let order = orderreview[indexPath.item]
cell.textBubbleView.backgroundColor = UIColor(white: 0.95, alpha: 1)
cell.messageTextView.textAlignment = .natural
cell.messageTextView.textColor = .black
cell.messageTextView.text = order.order
cell.textBubbleViewRightAnchore?.isActive = false
cell.textBubbleViewLeftAnchore?.isActive = true
if let imgUrl = order.profileImg, imgUrl != "" {
cell.profileImg.loadImageUsingCacheWithUrlStringAndPlaceHolder(urlString: imgUrl)
}
cell.profileImg.isHidden = false
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsets(top: 8, left: 0, bottom: 2, right: 0)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
var height: CGFloat = 80
let order = orderreview[indexPath.item]
if let text = order.order, text != "" {
height = estmatedFrameForText(text: text).height + 20
}
let width = UIScreen.main.bounds.width
return CGSize(width: width, height: height)
}
private func estmatedFrameForText(text: String) -> CGRect {
let size = CGSize(width: 250, height: 1000)
let options = NSStringDrawingOptions.usesFontLeading.union(.usesDeviceMetrics)
let style = NSMutableParagraphStyle()
style.alignment = NSTextAlignment.natural
return NSAttributedString(string: text, attributes: [ NSAttributedString.Key.font: UIFont.systemFont(ofSize: 16), NSAttributedString.Key.paragraphStyle: style ]).boundingRect(with: size, options: options, context: nil)
}
}
class OrderReviewCell: UICollectionViewCell{
let messageTextView: UITextView = {
let tv = UITextView()
tv.font = UIFont.systemFont(ofSize: 16)
tv.textAlignment = .natural
tv.backgroundColor = .clear
tv.isUserInteractionEnabled = false
tv.translatesAutoresizingMaskIntoConstraints = false
return tv
}()
let textBubbleView: UIView = {
let view = UIView()
view.layer.cornerRadius = 15
view.layer.masksToBounds = true
view.backgroundColor = .red
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()
let profileImg: UIImageView = {
let img = UIImageView(image: imageLiteral(resourceName: "ProfileHolder"))
img.contentMode = .scaleAspectFill
img.layer.cornerRadius = 16
img.layer.masksToBounds = true
img.translatesAutoresizingMaskIntoConstraints = false
return img
}()
var textBubbleViewWidth: NSLayoutConstraint?
var textBubbleViewRightAnchore: NSLayoutConstraint?
var textBubbleViewLeftAnchore: NSLayoutConstraint?
override init(frame: CGRect) {
super.init(frame: frame)
addSubview(textBubbleView)
addSubview(messageTextView)
addSubview(profileImg)
setupLayout()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func setupLayout(){
profileImg.leftAnchor.constraint(equalTo: self.leftAnchor, constant: 8).isActive = true
profileImg.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
profileImg.widthAnchor.constraint(equalToConstant: 32).isActive = true
profileImg.heightAnchor.constraint(equalToConstant: 32).isActive = true
textBubbleView.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
textBubbleView.heightAnchor.constraint(equalTo: self.heightAnchor).isActive = true
textBubbleViewWidth = textBubbleView.widthAnchor.constraint(equalToConstant: 200)
textBubbleViewWidth?.isActive = true
textBubbleViewRightAnchore = textBubbleView.rightAnchor.constraint(equalTo: self.rightAnchor, constant: -8)
textBubbleViewRightAnchore?.isActive = true
textBubbleViewLeftAnchore = textBubbleView.leftAnchor.constraint(equalTo: profileImg.rightAnchor, constant: 8)
textBubbleViewLeftAnchore?.isActive = true
messageTextView.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
messageTextView.rightAnchor.constraint(equalTo: self.textBubbleView.rightAnchor).isActive = true
messageTextView.heightAnchor.constraint(equalTo: self.heightAnchor).isActive = true
messageTextView.leftAnchor.constraint(equalTo: self.textBubbleView.leftAnchor, constant: 8).isActive = true
}
}

Unable to simultaneously satisfy constraints. Adaptive cell height using UICollectionViewFlowLayout

I'm trying to make a news feed like app using UICollectionView with adaptive cell height. I found this tutorial and used the option "2. Solution for iOS 11+" for my code, which works perfectly fine. However, whenever I try to add more subviews to the cell and layout them as required, I get this error: "Unable to simultaneously satisfy constraints. Probably at least one of the constraints in the following list is one you don't want."
Below is my code which gives me an error.
NewsFeedViewController:
import UIKit
class NewsFeedViewController: UIViewController {
var friends: [Friend] = []
var allPosts: [Post?] = []
var shuffledPosts: [Post?] = []
var collectionView: UICollectionView = {
let layout = NewsFeedFlowLayout()
layout.estimatedItemSize = UICollectionViewFlowLayout.automaticSize // new
layout.scrollDirection = .vertical
let collection = UICollectionView(frame: .zero, collectionViewLayout: layout)
collection.backgroundColor = UIColor.systemRed
collection.isScrollEnabled = true
collection.contentInsetAdjustmentBehavior = .always
collection.translatesAutoresizingMaskIntoConstraints = false
return collection
}()
let cellID = "NewsFeedCollectionViewCell"
override func viewDidLoad() {
super.viewDidLoad()
getPosts()
view.addSubview(collectionView)
collectionView.delegate = self
collectionView.dataSource = self
collectionView.register(NewsFeedCollectionViewCell.self, forCellWithReuseIdentifier: cellID)
collectionView.pin(to: view)
}
func getPosts() {
friends = FriendFactory().friends
allPosts = friends.flatMap{$0.posts}
var tempPosts = allPosts
for _ in allPosts {
if let index = tempPosts.indices.randomElement() {
let post = tempPosts[index]
shuffledPosts.append(post)
tempPosts.remove(at: index)
}
}
}
}
extension NewsFeedViewController: UICollectionViewDataSource, UICollectionViewDelegate {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return shuffledPosts.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellID, for: indexPath) as! NewsFeedCollectionViewCell
let post = shuffledPosts[indexPath.row]
let friend = friends.first(where: {$0.identifier == post?.authorID})
let text = post?.postText
cell.configurePostText(postText: text!)
return cell
}
}
NewsFeedCollectionViewCell:
import UIKit
class NewsFeedCollectionViewCell: UICollectionViewCell {
var selectedPost = Post()
var likeBarView: LikeBarView = {
let view = LikeBarView()
view.backgroundColor = .systemIndigo
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()
var postTextLabel: UILabel = {
let label = UILabel()
label.font = label.font.withSize(20)
label.numberOfLines = 0
label.lineBreakMode = .byWordWrapping
label.backgroundColor = .systemYellow
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
override init(frame: CGRect) {
super.init(frame: frame)
addViews()
setupConstraints()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func addViews() {
addSubview(likeBarView)
addSubview(postTextLabel)
}
func configurePostText(postText: String) {
postTextLabel.text = postText
}
override func preferredLayoutAttributesFitting(_ layoutAttributes: UICollectionViewLayoutAttributes) -> UICollectionViewLayoutAttributes {
let layoutAttributes = super.preferredLayoutAttributesFitting(layoutAttributes)
layoutIfNeeded()
layoutAttributes.frame.size = systemLayoutSizeFitting(UIView.layoutFittingCompressedSize, withHorizontalFittingPriority: .required, verticalFittingPriority: .fittingSizeLevel)
return layoutAttributes
}
func setupConstraints() {
NSLayoutConstraint.activate([
likeBarView.topAnchor.constraint(equalTo: topAnchor, constant: 20),
likeBarView.leadingAnchor.constraint(equalTo: leadingAnchor),
likeBarView.trailingAnchor.constraint(equalTo: trailingAnchor),
likeBarView.heightAnchor.constraint(equalToConstant: 100),
postTextLabel.topAnchor.constraint(equalTo: likeBarView.bottomAnchor, constant: 20),
postTextLabel.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 20),
postTextLabel.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -20),
postTextLabel.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -20),
])
}
}
NewsFeedFlowLayout:
import UIKit
final class NewsFeedFlowLayout: UICollectionViewFlowLayout {
override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
let layoutAttributesObjects = super.layoutAttributesForElements(in: rect)?.map{ $0.copy() } as? [UICollectionViewLayoutAttributes]
layoutAttributesObjects?.forEach({ layoutAttributes in
if layoutAttributes.representedElementCategory == .cell {
if let newFrame = layoutAttributesForItem(at: layoutAttributes.indexPath)?.frame {
layoutAttributes.frame = newFrame
}
}
})
return layoutAttributesObjects
}
override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
guard let collectionView = collectionView else {
fatalError()
}
guard let layoutAttributes = super.layoutAttributesForItem(at: indexPath)?.copy() as? UICollectionViewLayoutAttributes else {
return nil
}
layoutAttributes.frame.origin.x = sectionInset.left
layoutAttributes.frame.size.width = collectionView.safeAreaLayoutGuide.layoutFrame.width - sectionInset.left - sectionInset.right
return layoutAttributes
}
}
I enclose the screenshot of the collection view I'm getting and the screenshot of the analysis of the error done here, which is saying that the height of the cell is not dynamic if I'm getting it right.
What am I doing wrong?
Adding this line of code to NewsFeedViewController silenced the error:
layout.estimatedItemSize = CGSize(width: 375, height: 200)

Cannot get indexPath and selection on Collection View

I'm facing a problem with my collection view. I'm trying to pick the indexPath of an item to show more info on it after the tap, but I can't get it and figure out why.
I tried to allows collection View selection, but nothing happened.
I also tried to add a tap gesture on the cell, but don't know how to get the indexPath from there.
I add the same code on another VC, where the collection view is on the entire screen, and it works ( here, the collection View is half the screen ).
Here is the (updated) code for the collection View :
class SearchRunnerVC: UIViewController {
//MARK: - Objects
var lastNameTextField = RSTextField(placeholder: "Nom du coureur", returnKeyType: .next,
keyboardType: .default)
var firstNameTextField = RSTextField(placeholder: "Prénom", returnKeyType: .next,
keyboardType: .default)
var numberTextField = RSTextField(placeholder: "Numéro de dossard", returnKeyType: .next,
keyboardType: .numberPad)
var raceTextField = RSTextField(placeholder: "Course", returnKeyType: .done,
keyboardType: .default)
var searchButton = RSButton(backgroundColor: .systemPink, title: "Rechercher")
var collectionView : UICollectionView! = nil
var emptyLabel = UILabel()
let hud = JGProgressHUD(style: .dark)
lazy var textFields = [lastNameTextField, firstNameTextField, numberTextField, raceTextField]
//MARK: - Properties
let padding = CGFloat(20)
let runnersCollection = Firestore.firestore().collection("Runner")
var runners = [Runner]()
//MARK: - Lifecycle Methods
override func viewDidLoad() {
super.viewDidLoad()
setupUI()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
navigationController?.navigationBar.prefersLargeTitles = true
title = "Rechercher"
}
}
extension SearchRunnerVC {
fileprivate func setupUI() {
configureSuperView()
configureTextFields()
configureCollectionView()
configureButton()
configureConstraints()
}
fileprivate func configureSuperView() {
view.backgroundColor = .systemBackground
let viewTap = UITapGestureRecognizer(target: self, action: #selector(hideKeyboard(_:)))
view.addGestureRecognizer(viewTap)
}
fileprivate func configureConstraints() {
for textField in textFields {
view.addSubview(textField)
textField.leftToSuperview(view.leftAnchor, offset: padding)
textField.rightToSuperview(view.rightAnchor, offset: -padding)
textField.height(50)
textField.delegate = self
}
firstNameTextField.topToSuperview(view.safeAreaLayoutGuide.topAnchor, offset: padding, usingSafeArea: true)
lastNameTextField.topToBottom(of: firstNameTextField, offset: padding)
numberTextField.topToBottom(of: lastNameTextField, offset: padding)
raceTextField.topToBottom(of: numberTextField, offset: padding)
searchButton.topToBottom(of: raceTextField, offset: padding)
searchButton.leftToSuperview(view.leftAnchor, offset: padding)
searchButton.rightToSuperview(view.rightAnchor, offset: -padding)
searchButton.height(50)
collectionView.topToBottom(of: searchButton, offset: padding)
collectionView.edgesToSuperview(excluding: .top)
}
fileprivate func configureTextFields() {
firstNameTextField.tag = 0
lastNameTextField.tag = 1
numberTextField.tag = 2
raceTextField.tag = 3
}
fileprivate func configureButton() {
view.addSubview(searchButton)
searchButton.addTarget(self, action: #selector(searchRunners), for: .touchUpInside)
}
fileprivate func createLayout() -> UICollectionViewLayout {
let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0),
heightDimension: .fractionalHeight(1.0))
let item = NSCollectionLayoutItem(layoutSize: itemSize)
let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1),
heightDimension: .estimated(100))
let group = NSCollectionLayoutGroup.horizontal(layoutSize: groupSize, subitem: item, count: 2)
let spacing = CGFloat(10)
group.interItemSpacing = .fixed(spacing)
let section = NSCollectionLayoutSection(group: group)
section.interGroupSpacing = spacing
let padding = CGFloat(10)
section.contentInsets = NSDirectionalEdgeInsets(top: 0, leading: padding, bottom: 0, trailing: padding)
let headerSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1), heightDimension: .estimated(80))
let header = NSCollectionLayoutBoundarySupplementaryItem(layoutSize: headerSize, elementKind: UICollectionView.elementKindSectionHeader, alignment: .top)
section.boundarySupplementaryItems = [header]
let layout = UICollectionViewCompositionalLayout(section: section)
return layout
}
fileprivate func configureCollectionView() {
collectionView = UICollectionView(frame: .zero, collectionViewLayout: createLayout())
collectionView.autoresizingMask = [.flexibleHeight]
collectionView.backgroundColor = .systemBackground
collectionView.delegate = self
collectionView.dataSource = self
collectionView.register(RunnerCell.self, forCellWithReuseIdentifier: RunnerCell.reuseID)
collectionView.register(Header.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: Header.reuseID)
view.addSubview(collectionView)
}
}
extension SearchRunnerVC : UICollectionViewDelegate, UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return runners.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: RunnerCell.reuseID, for: indexPath) as? RunnerCell else { fatalError("Unable to dequeue runner cell")}
cell.configure(runner: runners[indexPath.row])
return cell
}
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
guard let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: Header.reuseID, for: indexPath) as? Header else { fatalError("Unable to dequeue header")}
header.title.text = "Résultats"
header.seeButton.addTarget(self, action: #selector(seeAllTapped), for: .touchUpInside)
header.separator.alpha = 0
header.backgroundColor = collectionView.backgroundColor
return header
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
print(indexPath.item)
}
}
Seems like your problem relates to the UITapGestureRecognizer you are adding to your view in configureSuperView(). The touches that would usually trigger the collectionView didSelectItemAt... delegate function are being sent to the gesture recognizer's handler, which is hideKeyboard().
Comment the line with view.addGestureRecognizer(viewTap) and your code will work. If so, I can also help you with achieving the hideKeyboard functionality, just let me know.
Do not use tap gesture on cells because
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
does excatly what you expect. The problem is probably in your collectionView configuration/setup. Could you show the whole ViewController code?

How do I access content of a uicollectionviewcell in a view controller?

I'm trying add a bottom border to a textfield inside a UICollectionViewCell, I registered the cell inside a view controller where my collection view is. But to set the size of the bottom border I need to the it own size, and I don't know how to do it inside the collection view cell, so Im trying to pass It to the view controller where It Is registered, but no success yet.
*Obs: I cut out some parts of the code because is not relevant.
UICollectionViewCell
class NameStepCell: UICollectionViewCell {
let safeAreaHolder: UIView = {
let view = UIView()
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()
let title: UILabel = {
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.numberOfLines = 0
label.font = UIFont.boldSystemFont(ofSize: 40)
label.text = "What is\nyour\nname?"
return label
}()
let txtFieldStack: UIStackView = {
let stack = UIStackView()
stack.translatesAutoresizingMaskIntoConstraints = false
stack.alignment = .center
stack.axis = .horizontal
stack.distribution = .fillEqually
stack.spacing = 20
return stack
}()
let nameField: UITextField = {
let txtFld = UITextField()
txtFld.keyboardType = UIKeyboardType.default
txtFld.textContentType = UITextContentType.name
txtFld.autocapitalizationType = UITextAutocapitalizationType.words
txtFld.autocorrectionType = .no
txtFld.textColor = UIColor.black
return txtFld
}()
let lastNameField: UITextField = {
let txtFld = UITextField()
txtFld.keyboardType = UIKeyboardType.default
txtFld.textContentType = UITextContentType.familyName
txtFld.autocapitalizationType = UITextAutocapitalizationType.words
txtFld.autocorrectionType = .no
txtFld.textColor = UIColor.black
return txtFld
}()
override init(frame: CGRect) {
super.init(frame: frame)
configuringView()
configuringTitle()
configuringTxtField()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func configuringView(){
addSubview(safeAreaHolder)
safeAreaHolder.topAnchor.constraint(equalTo: topAnchor).isActive = true
safeAreaHolder.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -16).isActive = true
safeAreaHolder.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
safeAreaHolder.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 16).isActive = true
}
func configuringTitle(){
safeAreaHolder.addSubview(title)
title.topAnchor.constraint(equalTo: safeAreaHolder.topAnchor, constant: 50).isActive = true
title.trailingAnchor.constraint(equalTo: safeAreaHolder.trailingAnchor).isActive = true
title.leadingAnchor.constraint(equalTo: safeAreaHolder.leadingAnchor).isActive = true
}
func configuringTxtField(){
safeAreaHolder.addSubview(txtFieldStack)
txtFieldStack.topAnchor.constraint(equalTo: title.bottomAnchor, constant: 50).isActive = true
txtFieldStack.trailingAnchor.constraint(equalTo: safeAreaHolder.trailingAnchor).isActive = true
txtFieldStack.leadingAnchor.constraint(equalTo: safeAreaHolder.leadingAnchor).isActive = true
txtFieldStack.addArrangedSubview(nameField)
txtFieldStack.addArrangedSubview(lastNameField)
nameField.heightAnchor.constraint(equalToConstant: 45).isActive = true
lastNameField.heightAnchor.constraint(equalToConstant: 45).isActive = true
}
}
UIViewController
class SignupViewController: UIViewController, UICollectionViewDelegateFlowLayout, UICollectionViewDelegate, UICollectionViewDataSource{
let stepsCollectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .horizontal
let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
collectionView.translatesAutoresizingMaskIntoConstraints = false
collectionView.backgroundColor = .white
collectionView.contentInsetAdjustmentBehavior = UIScrollView.ContentInsetAdjustmentBehavior.never
collectionView.isPagingEnabled = true
collectionView.showsHorizontalScrollIndicator = false
collectionView.isScrollEnabled = false
return collectionView
}()
override func viewDidLoad() {
super.viewDidLoad()
stepsCollectionView.dataSource = self
stepsCollectionView.delegate = self
stepsCollectionView.register(NameStepCell.self, forCellWithReuseIdentifier: "nameStepId")
stepsCollectionView.register(GenderStepCell.self, forCellWithReuseIdentifier: "genderStepId")
stepsCollectionView.register(BirthdayStepCell.self, forCellWithReuseIdentifier: "birthdayStepId")
stepsCollectionView.register(EmailStepCell.self, forCellWithReuseIdentifier: "emailStepId")
stepsCollectionView.register(PasswordStepCell.self, forCellWithReuseIdentifier: "passwordStepId")
view.backgroundColor = .white
configuringBottomButton()
configuringStepCollectionView()
}
override func viewDidAppear(_ animated: Bool) {
}
Here is where I try to get the nameFied to add the border
override func viewDidLayoutSubviews() {
NameStepCell().self.nameField.addBottomBorder()
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 5
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if indexPath.item == 1 {
let genderCell = collectionView.dequeueReusableCell(withReuseIdentifier: "genderStepId", for: indexPath)
return genderCell
}else if indexPath.item == 2{
let birthdayCell = collectionView.dequeueReusableCell(withReuseIdentifier: "birthdayStepId", for: indexPath)
return birthdayCell
}else if indexPath.item == 3{
let emailCell = collectionView.dequeueReusableCell(withReuseIdentifier: "emailStepId", for: indexPath)
return emailCell
}else if indexPath.item == 4{
let passwordCell = collectionView.dequeueReusableCell(withReuseIdentifier: "passwordStepId", for: indexPath)
return passwordCell
}
let nameCell = collectionView.dequeueReusableCell(withReuseIdentifier: "nameStepId", for: indexPath)
return nameCell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: stepsCollectionView.frame.size.width, height: stepsCollectionView.frame.size.height)
}
}
Extension to textfield to add the bottom border
extension UITextField {
func addBottomBorder() {
let border = CALayer()
border.frame = CGRect(x: 0, y: 32, width: self.frame.size.width, height: 1)
border.cornerRadius = 2
border.masksToBounds = true
border.backgroundColor = UIColor.init(red: 112/255, green: 112/255, blue: 112/255, alpha: 1).cgColor
self.layer.masksToBounds = true
self.layer.addSublayer(border)
}
}
Instead of calling addBottomBar() inside viewDidLayoutSubviews, you can try something like this.
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if indexPath.item == 1 {
let genderCell = collectionView.dequeueReusableCell(withReuseIdentifier: "genderStepId", for: indexPath)
return genderCell
}else if indexPath.item == 2{
let birthdayCell = collectionView.dequeueReusableCell(withReuseIdentifier: "birthdayStepId", for: indexPath)
return birthdayCell
}else if indexPath.item == 3{
let emailCell = collectionView.dequeueReusableCell(withReuseIdentifier: "emailStepId", for: indexPath)
return emailCell
}else if indexPath.item == 4{
let passwordCell = collectionView.dequeueReusableCell(withReuseIdentifier: "passwordStepId", for: indexPath)
return passwordCell
}
// Dequeue your NameStepCell from collection view
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "nameStepId", for: indexPath)
if let nameCell = cell as? NameStepCell {
// Add bottom border to it right here and return it
nameCell.nameField.addBottomBorder()
return nameCell
}
return cell
}
Edit:
Now you don't need to change anything in your SignUpViewController. Please replace your NameStepCell class with the below code.
class NameStepCell: UICollectionViewCell {
var safeAreaHolder: UIView!
var title: UILabel!
var txtFieldStack: UIStackView!
var nameField: UITextField!
var lastNameField: UITextField!
override init(frame: CGRect) {
super.init(frame: frame)
configuringView()
configuringTitle()
configuringTxtField()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
private extension NameStepCell {
func configuringView(){
let view = UIView()
view.translatesAutoresizingMaskIntoConstraints = false
self.safeAreaHolder = view
addSubview(safeAreaHolder)
safeAreaHolder.topAnchor.constraint(equalTo: topAnchor).isActive = true
safeAreaHolder.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -16).isActive = true
safeAreaHolder.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
safeAreaHolder.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 16).isActive = true
self.safeAreaHolder.layoutIfNeeded()
}
func configuringTitle(){
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.numberOfLines = 0
label.font = UIFont.boldSystemFont(ofSize: 40)
label.text = "What is\nyour\nname?"
self.title = label
safeAreaHolder.addSubview(title)
title.topAnchor.constraint(equalTo: safeAreaHolder.topAnchor, constant: 50).isActive = true
title.trailingAnchor.constraint(equalTo: safeAreaHolder.trailingAnchor).isActive = true
title.leadingAnchor.constraint(equalTo: safeAreaHolder.leadingAnchor).isActive = true
self.title.layoutIfNeeded()
}
func configuringTxtField(){
let stack = UIStackView()
stack.backgroundColor = .lightGray
stack.translatesAutoresizingMaskIntoConstraints = false
stack.alignment = .center
stack.axis = .horizontal
stack.distribution = .fillEqually
stack.spacing = 20
self.txtFieldStack = stack
safeAreaHolder.addSubview(txtFieldStack)
txtFieldStack.topAnchor.constraint(equalTo: title.bottomAnchor, constant: 50).isActive = true
txtFieldStack.trailingAnchor.constraint(equalTo: safeAreaHolder.trailingAnchor).isActive = true
txtFieldStack.leadingAnchor.constraint(equalTo: safeAreaHolder.leadingAnchor).isActive = true
self.txtFieldStack.layoutIfNeeded()
self.nameField = getTextField(.name)
self.lastNameField = getTextField(.familyName)
txtFieldStack.addArrangedSubview(nameField)
txtFieldStack.addArrangedSubview(lastNameField)
nameField.heightAnchor.constraint(equalToConstant: 45).isActive = true
lastNameField.heightAnchor.constraint(equalToConstant: 45).isActive = true
// After adding constraints, you should call 'layoutIfNeeded()' which recomputes the size and position based on the constraints you've set
self.nameField.layoutIfNeeded()
self.lastNameField.layoutIfNeeded()
self.nameField.addBottomBorder()
self.lastNameField.addBottomBorder()
}
func getTextField(_ textContentType: UITextContentType) -> UITextField {
let textField = UITextField()
textField.keyboardType = .default
textField.textContentType = textContentType
textField.autocapitalizationType = .words
textField.autocorrectionType = .no
textField.textColor = .black
textField.placeholder = textContentType.rawValue // P.S. Remove placeholder if you don't need.
return textField
}
}

The image within my custom UICollectionViewCell does not extend to the entire bounds of the cell?

I am trying to laod an array of images to a collectionview that is within a UIViewController. The UIImageView within my custom UICollectionViewCell is not extending to the bounds of the cell and I do not understand why. Below is my code.
ViewController:
var dragCollectionView: UICollectionView!
var dragCollectionViewWidth: CGFloat!
let borderInset: CGFloat = 3
var interCellSpacing: CGFloat = 3
var spacingBetweenRows: CGFloat = 3
let container1: UIView = {
let v = UIView()
v.backgroundColor = UIColor.yellow
return v
}()
override func viewDidLoad() {
super.viewDidLoad()
margins = view.layoutMarginsGuide
view.backgroundColor = UIColor.white
view.addSubview(container1)
container1.translatesAutoresizingMaskIntoConstraints = false
container1.heightAnchor.constraint(equalTo: margins.heightAnchor, multiplier: 0.5).isActive = true
container1.leadingAnchor.constraint(equalTo: margins.leadingAnchor).isActive = true
container1.trailingAnchor.constraint(equalTo: margins.trailingAnchor).isActive = true
container1.topAnchor.constraint(equalTo: margins.topAnchor).isActive = true
setUpCollectionViewForPuzzlePieces()
}
override func viewDidLayoutSubviews() {
print("")
print("viewDidLayoutSubviews ...")
dragCollectionViewWidth = dragCollectionView.frame.width
dragCollectionView.translatesAutoresizingMaskIntoConstraints = false
dragCollectionView.topAnchor.constraint(equalTo: container1.topAnchor).isActive = true
dragCollectionView.bottomAnchor.constraint(equalTo: container1.bottomAnchor).isActive = true
dragCollectionView.leftAnchor.constraint(equalTo: container1.leftAnchor).isActive = true
dragCollectionView.rightAnchor.constraint(equalTo: container1.rightAnchor).isActive = true
setupLayoutDragCollectionView()
}
private func setUpCollectionViewForPuzzlePieces(){
let frame = CGRect.init(x: 0, y: 0, width: 100, height: 100)
layout = UICollectionViewFlowLayout.init()
dragCollectionView = UICollectionView.init(frame: frame, collectionViewLayout: layout)
dragCollectionView.backgroundColor = UIColor.gray
dragCollectionView.delegate = self
dragCollectionView.dataSource = self
container1.addSubview(dragCollectionView)
dragCollectionView.translatesAutoresizingMaskIntoConstraints = false
dragCollectionView.topAnchor.constraint(equalTo: container1.topAnchor).isActive = true
dragCollectionView.bottomAnchor.constraint(equalTo: container1.bottomAnchor).isActive = true
dragCollectionView.leftAnchor.constraint(equalTo: container1.leftAnchor).isActive = true
dragCollectionView.rightAnchor.constraint(equalTo: container1.rightAnchor).isActive = true
// Register UICollectionViewCell
dragCollectionView.register(GirdyPickCollectionCell.self, forCellWithReuseIdentifier: cellId)
}
private func setupLayoutDragCollectionView(){
layout.minimumInteritemSpacing = interCellSpacing
layout.minimumLineSpacing = spacingBetweenRows
layout.sectionInset = UIEdgeInsets.init(top: borderInset, left: borderInset, bottom: borderInset, right: borderInset)
guard
let collectionViewWidth = dragCollectionViewWidth,
let numRows = numberOfGridColumns else{return}
print("numRows: \(numRows)")
let cellWidth = getCellWidth(numRows: numRows, collectionViewWidth: collectionViewWidth, interCellSpace: interCellSpacing, borderInset: borderInset)
layout.estimatedItemSize = CGSize.init(width: cellWidth, height: cellWidth)
print("margins.layoutFrame.width: \(margins.layoutFrame.width)")
print("collectionViewWidth: \(collectionViewWidth)")
print("cellWidth: \(cellWidth)")
print("")
}
private func getCellWidth(numRows: CGFloat, collectionViewWidth: CGFloat, interCellSpace: CGFloat, borderInset: CGFloat) -> CGFloat{
let numSpacing = numRows - 1
let cellWidth = (collectionViewWidth - interCellSpace * numSpacing - borderInset * 2) / numRows
return cellWidth
}
extension StartGameViewController: UICollectionViewDataSource{
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
guard let data = puzzlePieces else {return 0}
return data.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = dragCollectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! GirdyPickCollectionCell
guard let data = puzzlePieces else {return cell}
cell.imageView.image = data[indexPath.row].image
return cell
}
}
My custom UICollectionViewCell:
class GirdyPickCollectionCell: UICollectionViewCell {
var image: UIImage?
var imageView: UIImageView = {
let imgView = UIImageView()
return imgView
}()
override init(frame: CGRect) {
super.init(frame: frame)
self.addSubview(imageView)
self.backgroundColor = UIColor.red.withAlphaComponent(0.3)
setUpLayout()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
private func setUpLayout(){
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
imageView.widthAnchor.constraint(equalToConstant: frame.width).isActive = true
imageView.heightAnchor.constraint(equalToConstant: frame.height).isActive = true
}
}
CONSOLE:
viewDidLayoutSubviews ...
numRows: 5.0
margins.layoutFrame.width: 343.0
collectionViewWidth: 100.0
cellWidth: 16.4
viewDidLayoutSubviews ...
numRows: 5.0
margins.layoutFrame.width: 343.0
collectionViewWidth: 343.0
cellWidth: 65.0
What I am currently getting:
Implement UICollectionViewDelegateFlowLayout in your view controller and provide the size in collectionView:layout:sizeForItemAtIndexPath:
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAt indexPath: IndexPath) -> CGSize {
let kWhateverHeightYouWant = 100
return CGSize(width: collectionView.bounds.size.width, height: CGFloat(kWhateverHeightYouWant))
}
You can add constraints to Left (or leading), Top, Right (or trailing) and bottom of the imageView.
This will also help you to set padding between the frames if required.
Use this code:
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.rightAnchor.constraint(equalTo: self.rightAnchor).isActive = true
imageView.leftAnchor.constraint(equalTo: self.leftAnchor).isActive = true
imageView.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
imageView.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
I think you should set all edges constraints instead of giving width and height,
Try to add constraints like this
private func setUpLayout(){
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
imageView.leftAnchor.constraint(equalTo: leftAnchor).isActive = true
imageView.rightAnchor.constraint(equalTo: rightAnchor).isActive = true
imageView.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
}