Last active
May 11, 2020 13:32
-
-
Save MateusDeSousa/0dd235a86f4f7f88578805022d770ba2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class HomeCardViewCell: UITableViewCell { | |
private let cardContainer: UIView = { | |
let view = UIView() | |
view.translatesAutoresizingMaskIntoConstraints = false | |
view.backgroundColor = .white | |
return view | |
}() | |
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) { | |
super.init(style: style, reuseIdentifier: reuseIdentifier) | |
addSubviews() | |
setupAnchors() | |
contentView.backgroundColor = UIColor(red: 245/255, green: 246/255, blue: 252/255, alpha: 1) | |
} | |
required init?(coder: NSCoder) { | |
fatalError("init(coder:) has not been implemented") | |
} | |
override func layoutSubviews() { | |
super.layoutSubviews() | |
cardContainer.cornerRadius(25) | |
cardContainer.applyShadow() | |
} | |
private func addSubviews() { | |
contentView.addSubview(cardContainer) | |
} | |
private func setupAnchors() { | |
NSLayoutConstraint.activate([ | |
cardContainer.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 20), | |
cardContainer.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 10), | |
cardContainer.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -20), | |
cardContainer.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -10), | |
cardContainer.heightAnchor.constraint(equalToConstant: 200) | |
]) | |
} | |
} | |
class HomeController: UIViewController, UITableViewDelegate, UITableViewDataSource { | |
private var customView: HomeViewBase { | |
view as! HomeViewBase | |
} | |
override func loadView() { | |
super.loadView() | |
let customView = HomeViewBase() | |
customView.delegate = self | |
customView.dataSource = self | |
view = customView | |
} | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
customView.setDelegateAndDataSource() | |
} | |
override func viewWillAppear(_ animated: Bool) { | |
super.viewWillAppear(animated) | |
setupNavigation() | |
} | |
private func setupNavigation() { | |
title = "Test" | |
navigationController?.navigationBar.barTintColor = .systemBlue | |
navigationController?.navigationBar.titleTextAttributes = [.foregroundColor: UIColor.white] | |
navigationController?.navigationBar.tintColor = .white | |
} | |
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { | |
15 | |
} | |
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { | |
let cell = tableView.dequeueReusableCell(withIdentifier: "\(HomeCardViewCell.self)", for: indexPath) | |
return cell | |
} | |
} | |
extension UIView { | |
func cornerRadius(_ value: CGFloat) { | |
clipsToBounds = true | |
layer.cornerRadius = value | |
} | |
func applyShadow(color: UIColor = .black, opacity: Float = 0.1) { | |
layer.masksToBounds = false | |
layer.shadowColor = color.cgColor | |
layer.shadowOpacity = opacity | |
layer.shadowRadius = 10 | |
layer.shadowOffset = CGSize(width: 0, height: 10) | |
layer.shouldRasterize = true | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment