Swift iOS - Tag collection view
I resolve this problem, using collection view.
class FilterController: UIViewController, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {
@IBOutlet var collectionView: UICollectionView?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 150, left: 10, bottom: 150, right: 10)
// layout.itemSize = CGSize(width: 90, height: 45)
layout.itemSize = CGSizeFromString("Aloha")
collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
collectionView!.dataSource = self
collectionView!.delegate = self
collectionView!.registerClass(TagCell.self, forCellWithReuseIdentifier: "TagCell")
collectionView!.backgroundColor = UIColor.whiteColor()
self.view.addSubview(collectionView!)
}
iOS: Collection view custom tag layout dynamic Height Swift
@iDeveloper I tried to fix your code.
Here are the things you can do.
- Simplify your array.
let titles = ["Apple","Google","Computer", "Terminal", "Gross", "Form lands", "Water lands", "river", "mounts", "trees", "places", "parks", "towns", "cities","Lorem Ipsum is simply dummy text of the printing and typesettingindustry.","Hello","A","B","CAD","John","Nick","Lucas","Amy","Lawrance","Apple","Google","Computer", "Browser","Overflow","Hi","Hello","A","B","CAD","John","Nick","Lucas","Amy","Lawrance", "Lorem Ipsum is simply dummy text of the printing and typesetting industry.",""]
Setup a collectionView with tag-like cells
Maybe it will help you. I think you should check if you item stick out the collection view right inset.
In layoutAttributesForElements
method check this:
let collectionViewWidth = self.collectionView?.frame.width - (self.sectionInset.right + self.sectionInset.left)
if (attributes.frame.origin.x + attributes.frame.size.width > collectionViewWidth) {
var newLeftAlignedFrame = attributes.frame
newLeftAlignedFrame.origin.x = self.sectionInset.left
attributes.frame = newLeftAlignedFrame
}
Updated my answer and it works for me, you can see it on screenshot
multiple labels & ImageView in CollectionView iOS
It is better to size items of the collection inside the layout mechanism. So you may want to create a custom FlowLayout
and make it divide the available with by 3:
class WeatherCollectionViewFlowLayout: UICollectionViewFlowLayout {
override func prepare() {
super.prepare()
guard let collectionView = collectionView else { return assertionFailure("Collection view not found") }
let availableWidth = collectionView.bounds.width
let availableHeight = collectionView.bounds.height
let itemWidth: CGFloat = (availableWidth/3).rounded(.down)
itemSize = CGSize(width: itemWidth, height: availableHeight)
sectionInset = .zero
minimumLineSpacing = .zero
minimumInteritemSpacing = .zero
}
}
Then you should pass it as the layout of the collectionView
.
In Storyboard:
or Code:
CollectionView(frame: frame, collectionViewLayout: WeatherCollectionViewFlowLayout())
So no need for any fixed width.
Some notes
- Don't forget the state where items are more than the desired count!
- Don't forget to disable bouncing if you want it to just be fitted!
- Don't forget to update the
availableWidth
(oravailableHeight
) if you want to play with spacings - Don't repeat yourself! Exclude all repetitive data:
let forecast = self.HourlyData[indexPath.row]
cell.forecastHourlyTemp.text = "\(forecast.temp)°C"
cell.forecastHourlyTime.text = forecast.dt.fromUnixTimeToTime()
cell.forecastHourlyWeatherIcon.image = UIImage(named: forecast.weather[0].icon)
- Use
lowerCamelcase
for everything you define in your code except fortype
s:
HourlyData -> hourlyData
- Get rid of
self
where ever you can! - Get rid of
return
keyword where ever you can:
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { collection.count }
- Try NOT to use delegate methods for sizing collection items if you already know the size!
How do I provide tags for my collectionView UITextFields programmatically?
In cellForItemAt
delegate set you cell.inputText.delegate = self
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell : CollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionViewCell", for: indexPath) as! CollectionViewCell // REPLACE YOUR CELL NAME WITH CollectionViewCell
cell.inputText.delegate = self
return cell
}
Now you can get tapped textview data by using textViewDidBeginEditing
delegate like below.
extension ViewController: UITextViewDelegate { // REPLACE ViewController with Your ViewController Name
func textViewDidBeginEditing(_ textView: UITextView) {
var cell: CollectionViewCell?
cell = textView.superview?.superview as? CollectionViewCell
print(cell?.txtVW.text as! String)
}
}
Related Topics
How to Get Label Name from Button
Replaykit Startrecording Sometimes Never Enters Completion Handler
Swift, Parse.Com: How to Pass Data from Query
How to Calculate a Random Cgpoint Which Does Not Touch a Uiview
Skspritenode Does Not Let Touches Pass Through When Isuserinteractionenabled False
Swift How to "Pass by Value" of a Object
Alpha for Background Color Not Working in iOS
App Crashes When User Starts Typing in Uisearchbar
7 Duplicate Symbols for Architecture X86_64 in React Native
Two Uibezierpaths Intersection as a Uibezierpath
Swift Continuous Rotation Animation Not So Continuous
Swift Add Line Above to Control
My Uiviews Muck-Up When I Combine Uipangesturerecognizer and Autolayout
Swift Nfc Mifare - Nfciso7816Apdu Sendmifare Command Not Supported