Rizwan Ahmed
Rizwan Ahmed

Reputation: 1292

How can I add line under Row of UICollectionView, Swift 5, Xcode

How can I add line under Row of UICollectionView, Swift 5, Xcode?

I want separate row by line. CollectionView with CollectionViewCell and in right side running on emulator.

Upvotes: 3

Views: 4164

Answers (2)

Dris
Dris

Reputation: 844

  1. As Ayazmon said, you can add UIView to you cell with height of 1.

  2. You can add bottom border to your cell.

    extension CALayer {
    
    func addBorder(edge: UIRectEdge, color: UIColor, thickness: CGFloat) {
    
        let border = CALayer()
    
        switch edge {
        case .top:
            border.frame = CGRect(x: 0, y: 0, width: frame.width, height: thickness)
        case .bottom:
            border.frame = CGRect(x: 0, y: frame.height - thickness, width: frame.width, height: thickness)
        case .left:
            border.frame = CGRect(x: 0, y: 0, width: thickness, height: frame.height)
        case .right:
            border.frame = CGRect(x: frame.width - thickness, y: 0, width: thickness, height: frame.height)
        default:
            break
        }
        border.backgroundColor = color.cgColor
        addSublayer(border)
        }
    }
    

Then

cell.layer.addBorder(edge: .bottom, color: .black, thickness: 1)

Upvotes: 4

Ayazmon
Ayazmon

Reputation: 1380

Since UICollectionView doesn't have separators for it's cells you can either switched to a UITableView only if it's suitable for your page's design or you can add a separator within the UICollectionViewCell by using a UIView at the bottom with height of 1. There are also some third party solutions you might try which you can find on GitHub such as these: Solution 1, Solution 2.

Upvotes: 3

Related Questions