S.M_Emamian
S.M_Emamian

Reputation: 17383

How to add just bottom and right border to custom CollectionView

There are many questions in stackoverflow about add border to custom CollectionView.

but I cannot see any question about add border to arbitrary sides.

How to add just bottom and right border to custom CollectionView?

Upvotes: 1

Views: 1252

Answers (1)

Frankie
Frankie

Reputation: 11928

Use an extension like this to add a border to any UIView.

extension UIView {
    func addBorder(_ width: CGFloat, color: UIColor, alpha: CGFloat) {
         let border = CALayer()
         border.borderColor = color.withAlphaComponent(alpha).cgColor
         border.borderWidth = borderWidth
         border.frame = CGRect(x: 0 - borderWidth, y: 0 - borderWidth, width: self.frame.size.width + borderWidth, height: self.frame.size.height - borderWidth)
         self.layer.addSublayer(border)
         self.layer.masksToBounds = true
    }
}

This should create a bottom and right side border. It adds a new layer to the view, and creates a border on that layer. You can then manipulate the frame of the border to show the edges you want to show.

Usage:

collectionView.addBorder(2, color: .red, alpha: 0.8)

Upvotes: 1

Related Questions