swift输出一个登陆界面app代码
时间: 2024-05-10 08:21:18 浏览: 131
以下是一个简单的登陆界面app代码,使用Swift语言编写:
```swift
import UIKit
class LoginViewController: UIViewController {
// MARK: - Properties
let logoImageView: UIImageView = {
let imageView = UIImageView()
imageView.image = UIImage(named: "logo")
imageView.contentMode = .scaleAspectFit
imageView.translatesAutoresizingMaskIntoConstraints = false
return imageView
}()
let emailTextField: UITextField = {
let textField = UITextField()
textField.placeholder = "Email"
textField.borderStyle = .roundedRect
textField.translatesAutoresizingMaskIntoConstraints = false
return textField
}()
let passwordTextField: UITextField = {
let textField = UITextField()
textField.placeholder = "Password"
textField.borderStyle = .roundedRect
textField.isSecureTextEntry = true
textField.translatesAutoresizingMaskIntoConstraints = false
return textField
}()
let loginButton: UIButton = {
let button = UIButton(type: .system)
button.setTitle("Login", for: .normal)
button.backgroundColor = .systemBlue
button.setTitleColor(.white, for: .normal)
button.layer.cornerRadius = 5
button.translatesAutoresizingMaskIntoConstraints = false
return button
}()
// MARK: - Lifecycle Methods
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
configureUI()
}
// MARK: - Helper Methods
func configureUI() {
view.addSubview(logoImageView)
view.addSubview(emailTextField)
view.addSubview(passwordTextField)
view.addSubview(loginButton)
logoImageView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
logoImageView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 50).isActive = true
logoImageView.widthAnchor.constraint(equalToConstant: 150).isActive = true
logoImageView.heightAnchor.constraint(equalToConstant: 150).isActive = true
emailTextField.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
emailTextField.topAnchor.constraint(equalTo: logoImageView.bottomAnchor, constant: 50).isActive = true
emailTextField.widthAnchor.constraint(equalToConstant: 250).isActive = true
emailTextField.heightAnchor.constraint(equalToConstant: 40).isActive = true
passwordTextField.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
passwordTextField.topAnchor.constraint(equalTo: emailTextField.bottomAnchor, constant: 20).isActive = true
passwordTextField.widthAnchor.constraint(equalToConstant: 250).isActive = true
passwordTextField.heightAnchor.constraint(equalToConstant: 40).isActive = true
loginButton.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
loginButton.topAnchor.constraint(equalTo: passwordTextField.bottomAnchor, constant: 50).isActive = true
loginButton.widthAnchor.constraint(equalToConstant: 200).isActive = true
loginButton.heightAnchor.constraint(equalToConstant: 40).isActive = true
}
}
```
这个登陆界面包含一个Logo图片、一个Email输入框、一个密码输入框和一个登陆按钮。使用Auto Layout来设置它们的位置和大小。可以根据需要对它们进行修改和调整。
阅读全文