Skip to content

Instantly share code, notes, and snippets.

@amfathi
Last active July 15, 2022 10:35
Show Gist options
  • Select an option

  • Save amfathi/98f2e3426b5f25764751bbc73f350503 to your computer and use it in GitHub Desktop.

Select an option

Save amfathi/98f2e3426b5f25764751bbc73f350503 to your computer and use it in GitHub Desktop.
**Base** Views and Controllers (SnapKit + RxSwift)
import UIKit
class BaseCollectionViewCell: UICollectionViewCell, Identifiable {
override init(frame: CGRect) {
super.init(frame: .zero)
setupViews()
setupConstraints()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
}
// MARK: - Layout
/// Override to setup your views and subviews.
func setupViews() {}
/// Override to setup your views constraints.
func setupConstraints() {}
}
import UIKit
class BaseNavigationController: UINavigationController {
init() {
super.init(nibName: nil, bundle: nil)
}
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
super.init(nibName: nil, bundle: nil)
}
override init(rootViewController: UIViewController) {
super.init(rootViewController: rootViewController)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func viewDidLoad() {
super.viewDidLoad()
setupAppearance()
}
/// Setup navigationBar custom appearance
private func setupAppearance() {
navigationBar.tintColor = .white
navigationBar.barTintColor = .navigationBarTint
navigationBar.titleTextAttributes = [.foregroundColor: UIColor.white]
navigationBar.isTranslucent = false
}
}
import UIKit
class BaseTableViewCell: UITableViewCell, Identifiable {
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
setupViews()
setupConstraints()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
}
// MARK: - Layout
/// Override to setup your views and subviews.
func setupViews() {}
/// Override to setup your views constraints.
func setupConstraints() {}
}
import UIKit
class BaseView: UIView {
// MARK: - Initializers
override init(frame: CGRect) {
super.init(frame: .zero)
// Setting up subviews
setupViews()
setupConstraints()
setupContentData()
// Binding
bindViews()
bindActions()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
// MARK: - Layout
/// Override to setup your views and subviews.
func setupViews() {}
/// Override to setup your views constraints.
func setupConstraints() {}
/// Override to setup your static data to your views.
func setupContentData() {}
// MARK: - Binding
/// Override to bind your views to your view model.
func bindViews() {}
/// Override to bind your actions to your view model.
func bindActions() {}
}
import UIKit
class BaseViewController: UIViewController {
// MARK: - Lifecycle
// No need to implement `viewDidLoad`.
// Instead implement `setupViews()` and `setupConstraints()`, still no harm of implementing it.
override func viewDidLoad() {
super.viewDidLoad()
// Base Customization
setupBaseCustomization()
// Setting up views & content (Overrideable functions)
setupViews()
setupConstraints()
setupContentData()
// Binding
bindViews()
bindActions()
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
// force-close the keybaord when the viewController is disappearing
view.endEditing(true)
}
private func setupBaseCustomization() {
view.backgroundColor = .mainBackground
navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
}
// MARK: - Layout
/// Override to setup your views and subviews.
func setupViews() {}
/// Override to setup your views constraints.
func setupConstraints() {}
/// Override to setup your static data to your views.
func setupContentData() {}
// MARK: - Binding
/// Override to bind your views to your view model.
func bindViews() {}
/// Override to bind your actions to your view model.
func bindActions() {}
}
protocol Identifiable {
static var identifier: String { get }
}
extension Identifiable where Self: UITableViewCell {
static var identifier: String {
return self.classForCoder().description()
}
}
extension Identifiable where Self: UICollectionViewCell {
static var identifier: String {
return self.classForCoder().description()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment