Created
October 16, 2022 13:33
-
-
Save xmhafiz/42a8ac167bd1bf4d99b081ab9b9605c3 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| // | |
| // ViewController.swift | |
| // | |
| import UIKit | |
| class ViewController: UIViewController { | |
| // 1 | |
| lazy var stackView: UIStackView = { | |
| let view = UIStackView() | |
| view.axis = .vertical | |
| view.spacing = 8 | |
| view.translatesAutoresizingMaskIntoConstraints = false | |
| return view | |
| }() | |
| lazy var emailTextField: UITextField = { | |
| let textField = UITextField() | |
| textField.textContentType = .emailAddress | |
| textField.keyboardType = .emailAddress | |
| textField.autocorrectionType = .no | |
| textField.autocapitalizationType = .none | |
| textField.borderStyle = .roundedRect | |
| textField.placeholder = "Enter email.." | |
| return textField | |
| }() | |
| lazy var passwordTextField: UITextField = { | |
| let textField = UITextField() | |
| textField.borderStyle = .roundedRect | |
| textField.placeholder = "Enter password.." | |
| textField.isSecureTextEntry = true | |
| return textField | |
| }() | |
| lazy var submitButton: UIButton = { | |
| let button = UIButton(type: .system) | |
| button.setTitle("Login", for: .normal) | |
| button.setTitleColor(.lightGray, for: .disabled) | |
| button.addTarget(self, action: #selector(onSubmit), for: .touchUpInside) | |
| return button | |
| }() | |
| override func viewDidLoad() { | |
| super.viewDidLoad() | |
| setupViews() | |
| setupConstraints() | |
| } | |
| func setupViews() { | |
| // 2 | |
| view.addSubview(stackView) | |
| stackView.addArrangedSubview(emailTextField) | |
| stackView.addArrangedSubview(passwordTextField) | |
| stackView.addArrangedSubview(submitButton) | |
| } | |
| func setupConstraints() { | |
| // 3 | |
| NSLayoutConstraint.activate([ | |
| stackView.centerYAnchor.constraint(equalTo: view.centerYAnchor), | |
| stackView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 24), | |
| stackView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -24), | |
| emailTextField.heightAnchor.constraint(equalToConstant: 48), | |
| passwordTextField.heightAnchor.constraint(equalToConstant: 48), | |
| submitButton.heightAnchor.constraint(equalToConstant: 48) | |
| ]) | |
| } | |
| // 4 | |
| @objc func onSubmit() { | |
| // handle submit here | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment