Skip to content

Instantly share code, notes, and snippets.

View xmhafiz's full-sized avatar
🏠
Working from home

Mohd Hafiz xmhafiz

🏠
Working from home
View GitHub Profile
class BottomSheetViewController: UIViewController {
// ...
// sub-class of this view controller will call this function to set a content
func setContent(content: UIView) {
contentView.addSubview(content)
NSLayoutConstraint.activate([
content.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
content.trailingAnchor.constraint(equalTo: contentView.trailingAnchor),
content.topAnchor.constraint(equalTo: contentView.topAnchor),
content.bottomAnchor.constraint(equalTo: contentView.bottomAnchor)
class BottomSheetViewController: UIViewController {
// ...
private var minTopSpacing: CGFloat = 80
// MARK: - View Setup
override func viewDidLoad() {
super.viewDidLoad()
setupViews()
}
@xmhafiz
xmhafiz / medium_BottomSheetViewController_1.swift
Last active June 27, 2023 20:44
BottomSheetViewController part 1
import UIKit
class BottomSheetViewController: UIViewController {
// MARK: - UI
/// Main bottom sheet container view
private lazy var mainContainerView: UIView = {
let view = UIView()
view.translatesAutoresizingMaskIntoConstraints = false
view.backgroundColor = .systemBackground
view.layer.cornerRadius = 8
import UIKit
class ViewController: UIViewController {
// After connected from storyboard to this class
@IBOutlet weak var contentStackView: UIStackView!
override func viewDidLoad() {
super.viewDidLoad()
// Add GradientButton programatically
import UIKit
@IBDesignable
class GradientButton: UIButton {
// Define the colors for the gradient
@IBInspectable var startColor: UIColor = UIColor.red {
didSet {
updateGradient()
}
func showResultScreen() {
let homeVC = ResultViewController()
navigationController?.pushViewController(homeVC, animated: true)
}
//
// ResultViewController.swift
//
import UIKit
class ResultViewController: UIViewController {
lazy var messageLabel: UILabel = {
let label = UILabel()
label.text = "Login success"
func setupPublishers() {
// 1
NotificationCenter.default
.publisher(for: UITextField.textDidChangeNotification, object: emailTextField)
.map { ($0.object as! UITextField).text ?? "" }
.assign(to: \.email, on: viewModel)
.store(in: &cancellables)
NotificationCenter.default
func setupViews() {
view.addSubview(stackView)
stackView.addArrangedSubview(emailTextField)
stackView.addArrangedSubview(passwordTextField)
stackView.addArrangedSubview(submitButton)
stackView.addArrangedSubview(errorLabel) // append the error label here
}
lazy var errorLabel: UILabel = {
let label = UILabel()
label.text = "Oops, incorrect email or password!"
label.textColor = .white
label.backgroundColor = .red
label.textAlignment = .center
label.translatesAutoresizingMaskIntoConstraints = false
label.alpha = 0
return label
}()