To add rounded corners and a drop shadow to a UICollectionViewCell in your iOS application, you can use the layer property of the cell's content view. Here's how you can do it:
cornerRadius property of the cell's layer.shadowColor, shadowOpacity, shadowOffset, and shadowRadius.Here's a complete example in Swift:
import UIKit class CustomCollectionViewCell: UICollectionViewCell { override init(frame: CGRect) { super.init(frame: frame) setupCell() } required init?(coder: NSCoder) { super.init(coder: coder) setupCell() } private func setupCell() { // Add rounded corners contentView.layer.cornerRadius = 10 contentView.layer.masksToBounds = true // Add shadow layer.shadowColor = UIColor.black.cgColor layer.shadowOpacity = 0.5 layer.shadowOffset = CGSize(width: 0, height: 2) layer.shadowRadius = 4 layer.masksToBounds = false } override func layoutSubviews() { super.layoutSubviews() // Ensure the shadow path is updated to match the cell's bounds layer.shadowPath = UIBezierPath(roundedRect: bounds, cornerRadius: contentView.layer.cornerRadius).cgPath } } Rounded Corners:
contentView.layer.cornerRadius = 10: This sets the corner radius of the cell's content view to 10 points, giving it rounded corners.contentView.layer.masksToBounds = true: This ensures that the content view's subviews are clipped to its rounded corners.Drop Shadow:
layer.shadowColor = UIColor.black.cgColor: Sets the shadow color to black.layer.shadowOpacity = 0.5: Sets the shadow opacity to 50%.layer.shadowOffset = CGSize(width: 0, height: 2): Sets the shadow offset, i.e., the position of the shadow relative to the cell.layer.shadowRadius = 4: Sets the blur radius of the shadow.layer.masksToBounds = false: Ensures the shadow is not clipped to the cell's bounds.Shadow Path:
layoutSubviews(), the layer.shadowPath is updated to match the cell's bounds and corner radius, ensuring the shadow looks correct even when the cell is resized.In your UICollectionView data source, register and dequeue this custom cell:
collectionView.register(CustomCollectionViewCell.self, forCellWithReuseIdentifier: "CustomCell") func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CustomCell", for: indexPath) as! CustomCollectionViewCell // Configure the cell as needed return cell } This setup ensures that your UICollectionViewCell will have rounded corners and a drop shadow, enhancing the visual appearance of your collection view.
iPhone UICollectionViewCell rounded corners and drop shadow
UICollectionViewCell in iOS.// Swift cell.contentView.layer.cornerRadius = 10.0 cell.contentView.layer.borderWidth = 1.0 cell.contentView.layer.borderColor = UIColor.clear.cgColor cell.contentView.layer.masksToBounds = true cell.layer.shadowColor = UIColor.black.cgColor cell.layer.shadowOffset = CGSize(width: 0, height: 2.0) cell.layer.shadowRadius = 2.0 cell.layer.shadowOpacity = 0.5 cell.layer.masksToBounds = false cell.layer.shadowPath = UIBezierPath(roundedRect: cell.bounds, cornerRadius: cell.contentView.layer.cornerRadius).cgPath
cornerRadius, borderWidth, borderColor, and masksToBounds properties on cell.contentView to create rounded corners. Applies drop shadow using shadowColor, shadowOffset, shadowRadius, shadowOpacity, and shadowPath properties on the cell's layer.Objective-C rounded corners and drop shadow on UICollectionViewCell
UICollectionViewCell in Objective-C.// Objective-C cell.contentView.layer.cornerRadius = 10.0; cell.contentView.layer.borderWidth = 1.0; cell.contentView.layer.borderColor = [UIColor clearColor].CGColor; cell.contentView.layer.masksToBounds = YES; cell.layer.shadowColor = [UIColor blackColor].CGColor; cell.layer.shadowOffset = CGSizeMake(0, 2.0); cell.layer.shadowRadius = 2.0; cell.layer.shadowOpacity = 0.5; cell.layer.masksToBounds = NO; cell.layer.shadowPath = [UIBezierPath bezierPathWithRoundedRect:cell.bounds cornerRadius:cell.contentView.layer.cornerRadius].CGPath;
cornerRadius, borderWidth, borderColor, and masksToBounds for rounded corners. Sets up drop shadow with shadowColor, shadowOffset, shadowRadius, shadowOpacity, and shadowPath.Swift 5 UICollectionViewCell rounded corners with shadow
UICollectionViewCell in Swift 5.// Swift 5 cell.contentView.layer.cornerRadius = 10.0 cell.contentView.layer.borderWidth = 1.0 cell.contentView.layer.borderColor = UIColor.clear.cgColor cell.contentView.layer.masksToBounds = true cell.layer.shadowColor = UIColor.black.cgColor cell.layer.shadowOffset = CGSize(width: 0, height: 2.0) cell.layer.shadowRadius = 2.0 cell.layer.shadowOpacity = 0.5 cell.layer.masksToBounds = false cell.layer.shadowPath = UIBezierPath(roundedRect: cell.bounds, cornerRadius: cell.contentView.layer.cornerRadius).cgPath
cornerRadius, borderWidth, borderColor, and masksToBounds on cell.contentView. Applies drop shadow using shadowColor, shadowOffset, shadowRadius, shadowOpacity, and shadowPath on the cell's layer.Adding rounded corners to UICollectionViewCell Swift
UICollectionViewCell in Swift.// Swift cell.contentView.layer.cornerRadius = 10.0 cell.contentView.layer.borderWidth = 1.0 cell.contentView.layer.borderColor = UIColor.clear.cgColor cell.contentView.layer.masksToBounds = true
cornerRadius, borderWidth, borderColor, and masksToBounds properties on cell.contentView to create rounded corners without drop shadow.Objective-C add rounded corners to UICollectionViewCell
UICollectionViewCell in Objective-C.// Objective-C cell.contentView.layer.cornerRadius = 10.0; cell.contentView.layer.borderWidth = 1.0; cell.contentView.layer.borderColor = [UIColor clearColor].CGColor; cell.contentView.layer.masksToBounds = YES;
cornerRadius, borderWidth, borderColor, and masksToBounds properties on cell.contentView to achieve rounded corners.UICollectionViewCell drop shadow with rounded corners Swift
UICollectionViewCell in Swift.// Swift cell.layer.shadowColor = UIColor.black.cgColor cell.layer.shadowOffset = CGSize(width: 0, height: 2.0) cell.layer.shadowRadius = 2.0 cell.layer.shadowOpacity = 0.5 cell.layer.masksToBounds = false cell.layer.shadowPath = UIBezierPath(roundedRect: cell.bounds, cornerRadius: cell.contentView.layer.cornerRadius).cgPath
shadowColor, shadowOffset, shadowRadius, shadowOpacity, and shadowPath properties on the cell's layer, utilizing rounded corners from cell.contentView.Add drop shadow to UICollectionViewCell Objective-C
UICollectionViewCell in Objective-C.// Objective-C cell.layer.shadowColor = [UIColor blackColor].CGColor; cell.layer.shadowOffset = CGSizeMake(0, 2.0); cell.layer.shadowRadius = 2.0; cell.layer.shadowOpacity = 0.5; cell.layer.masksToBounds = NO; cell.layer.shadowPath = [UIBezierPath bezierPathWithRoundedRect:cell.bounds cornerRadius:cell.contentView.layer.cornerRadius].CGPath;
shadowColor, shadowOffset, shadowRadius, shadowOpacity, and shadowPath properties on the cell's layer, incorporating rounded corners from cell.contentView.UICollectionViewCell add border and drop shadow Swift
UICollectionViewCell in Swift.// Swift cell.contentView.layer.cornerRadius = 10.0 cell.contentView.layer.borderWidth = 1.0 cell.contentView.layer.borderColor = UIColor.lightGray.cgColor cell.contentView.layer.masksToBounds = true cell.layer.shadowColor = UIColor.black.cgColor cell.layer.shadowOffset = CGSize(width: 0, height: 2.0) cell.layer.shadowRadius = 2.0 cell.layer.shadowOpacity = 0.5 cell.layer.masksToBounds = false cell.layer.shadowPath = UIBezierPath(roundedRect: cell.bounds, cornerRadius: cell.contentView.layer.cornerRadius).cgPath
cornerRadius, borderWidth, borderColor, and masksToBounds on cell.contentView. Applies drop shadow with shadowColor, shadowOffset, shadowRadius, shadowOpacity, and shadowPath on the cell's layer.UICollectionViewCell add drop shadow only Swift
UICollectionViewCell in Swift.// Swift cell.layer.shadowColor = UIColor.black.cgColor cell.layer.shadowOffset = CGSize(width: 0, height: 2.0) cell.layer.shadowRadius = 2.0 cell.layer.shadowOpacity = 0.5 cell.layer.masksToBounds = false
shadowColor, shadowOffset, shadowRadius, shadowOpacity, and masksToBounds) on the cell's layer without configuring cornerRadius, borderWidth, or borderColor.Objective-C UICollectionViewCell rounded corners with shadow
UICollectionViewCell in Objective-C.// Objective-C cell.contentView.layer.cornerRadius = 10.0; cell.contentView.layer.borderWidth = 1.0; cell.contentView.layer.borderColor = [UIColor clearColor].CGColor; cell.contentView.layer.masksToBounds = YES; cell.layer.shadowColor = [UIColor blackColor].CGColor; cell.layer.shadowOffset = CGSizeMake(0, 2.0); cell.layer.shadowRadius = 2.0; cell.layer.shadowOpacity = 0.5; cell.layer.masksToBounds = NO; cell.layer.shadowPath = [UIBezierPath bezierPathWithRoundedRect:cell.bounds cornerRadius:cell.contentView.layer.cornerRadius].CGPath;
cornerRadius, borderWidth, borderColor, and masksToBounds for rounded corners on cell.contentView. Applies drop shadow with shadowColor, shadowOffset, shadowRadius, shadowOpacity, and shadowPath on the cell's layer.merge-conflict-resolution oracle-apex-5 tui selection windows-services synchronization jakarta-mail identityserver4 project contact-form-7