How can add line under Row of UICollectionView SWIFT5 XCODE

How can add line under Row of UICollectionView SWIFT5 XCODE

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

2 answers

  • answered 2020-02-19 11:56 Ayazmon

    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.

  • answered 2020-02-19 13:20 Dris

    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)