Skip to content

Instantly share code, notes, and snippets.

View nathangitter's full-sized avatar

Nathan Gitter nathangitter

View GitHub Profile
extension ViewController: UICollectionViewDelegate {
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
print("selected item at \(indexPath)")
}
}
extension ViewController: UICollectionViewDataSource {
class MyViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
var foods = ["🍎", "🍌", "🍓"]
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
print("selected item at \(indexPath)")
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return foods.count
@IBOutlet weak var myCustomView: UIView! {
didSet {
view.backgroundColor = UIColor.blue
view.layer.cornerRadius = myCustomView.bounds.width / 2
}
}
lazy var myCustomView: UIView = {
let view = UIView()
view.backgroundColor = UIColor.blue
view.layer.cornerRadius = myCustomView.bounds.width / 2
return view
}()
var myCustomView = UIView()
override func viewDidLoad() {
myCustomView.backgroundColor = UIColor.blue
myCustomView.layer.cornerRadius = myCustomView.bounds.width / 2
}
class MyButton: UIButton {
var action: (() -> ())?
override init(frame: CGRect) {
super.init(frame: frame)
sharedInit()
}
required init?(coder aDecoder: NSCoder) {
override func viewDidLoad() {
let button = MyButton()
button.action = {
print("MyButton pressed!")
}
view.addSubview(button)
}
override func viewDidLoad()
let button = UIButton()
button.addTarget(self, action: #selector(uiButtonAction), for: .touchUpInside)
view.addSubview(button)
}
@objc private func uiButtonAction() {
print("UIButton pressed!")
}