I want to draw line in between two Cell in UIcollectionView,
Below is design what exactly I want
Above design is dynamically change depends on array value.Please tell me if any one know how to draw line .
I want to draw line in between two Cell in UIcollectionView,
Below is design what exactly I want
Above design is dynamically change depends on array value.Please tell me if any one know how to draw line .
Well this is a request, as you've shown no approach so far, but here I am responding to your post with a solution which works with the collectionView
UICollectionView
in your controller which lets say has a height of 100pt
and its padding to [left, right, top] is 0. (Configurable via Autolayout)UICollectionView
add UICollectionViewCell
which has 3 elements
UIView
(Or your custom UIImageView
which will create your yellow line. It needs to be vertically centered, leading and trailing to superview equal to 0 and the height lets say 5pt
UIImageView
which will be centered and depending on the node, it will show start, progress or end node. It needs to be centered horizontally and vertically, and a custom height and width.UILabel
which will show the node names (if start or end it needs to be hidden). Put a leading and trailing margin to 8pt
and center it vertically.View tree should look something like this:
The idea is that each node of your route will be represented by a similar cell, and without leaving leaving spaces between UICollectionViewCell
s you will have a similar effect.
Note: I didn't create a custom cell, and that should be something that you have to do it on your own, where you will be able to change the content depending on your data. And let this be a task for you to learn :)
class ViewController: UIViewController, UICollectionViewDataSource {
@IBOutlet weak var collectionView: UICollectionView!
// MARK: - View Lifecycle
override func viewDidLoad() {
super.viewDidLoad()
setupCollectionView()
}
// MARK: - Configurations
private func setupCollectionView() {
// Set datasource
collectionView.dataSource = self
// Set flow layout
let layout:UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
layout.itemSize = CGSize(width: 100, height: 100.0)
layout.scrollDirection = .horizontal
layout.minimumInteritemSpacing = 0
layout.minimumLineSpacing = 0
collectionView.collectionViewLayout = layout
}
// MARK: - Protocol Conformance
// MARK: UICollectionViewDataSource
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 5
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier:"CellIdentifier", for: indexPath)
if indexPath.item == 0 {
// This is the start of the route
} else if indexPath.item == (collectionView.numberOfSections - 1) {
// This the the end of it
} else {
// Other nodes on your route
}
return cell
}
}
Option 1
Option 2
Option 3
This option allows you to be more creative. Maybe you want to dynamically change the distance between each cell with an animation? Or maybe you want to increase the gap between just a single pair of cells? To do this, you would setup multiple layouts that differ slightly in their implementation (or a single layout that is configurable) and swap between instances thereof.
This is my most recent custom collection view layout implementation. It's a grid layout with section header views, so not exactly what you want, but it should get you started.
Other Refs