Skip to content

Instantly share code, notes, and snippets.

@rlxone
Last active April 2, 2019 16:48
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rlxone/395b98814324ea97a8fb9dc074270deb to your computer and use it in GitHub Desktop.
Save rlxone/395b98814324ea97a8fb9dc074270deb to your computer and use it in GitHub Desktop.
iOS cards with cornerRadius and shadow in UICollectionView [Swift 4 Playground]
//: A UIKit based Playground for presenting user interface
import UIKit
import PlaygroundSupport
class CardView: UICollectionViewCell {
override init(frame: CGRect) {
super.init(frame: frame)
setupView()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
func setupView() {
backgroundColor = UIColor.white
let backgroundView = UIView()
backgroundView.backgroundColor = UIColor.red
backgroundView.layer.shadowRadius = 16
backgroundView.layer.shadowColor = UIColor.black.cgColor
backgroundView.layer.shadowOpacity = 1
backgroundView.layer.shadowOffset = CGSize(width: 0, height: 0)
backgroundView.translatesAutoresizingMaskIntoConstraints = false
addSubview(backgroundView)
let mainView = UIView()
mainView.backgroundColor = UIColor.lightGray
mainView.translatesAutoresizingMaskIntoConstraints = false
mainView.layer.cornerRadius = 16
addSubview(mainView)
var constraints = [NSLayoutConstraint]()
constraints += NSLayoutConstraint.constraints(withVisualFormat: "V:|-32-[backgroundView]-32-|", metrics: nil, views: ["backgroundView": backgroundView])
constraints += NSLayoutConstraint.constraints(withVisualFormat: "H:|-32-[backgroundView]-32-|", metrics: nil, views: ["backgroundView": backgroundView])
constraints += NSLayoutConstraint.constraints(withVisualFormat: "V:|-27-[mainView]-27-|", metrics: nil, views: ["mainView": mainView])
constraints += NSLayoutConstraint.constraints(withVisualFormat: "H:|-27-[mainView]-27-|", metrics: nil, views: ["mainView": mainView])
NSLayoutConstraint.activate(constraints)
}
}
class CardsCollectionViewController : UICollectionViewController {
override func viewDidLoad() {
super.viewDidLoad()
setupView()
}
func setupView() {
collectionView?.register(CardView.self, forCellWithReuseIdentifier: "CardView")
collectionView?.backgroundColor = UIColor.white
}
override func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 10
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cardView = collectionView.dequeueReusableCell(withReuseIdentifier: "CardView", for: indexPath)
return cardView
}
}
extension CardsCollectionViewController: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: collectionView.frame.width, height: 300)
}
}
// Present the view controller in the Live View window
PlaygroundPage.current.liveView = CardsCollectionViewController(collectionViewLayout: UICollectionViewFlowLayout())
PlaygroundPage.current.needsIndefiniteExecution = true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment