注册界面小Demo

主要思路:通过封装的思想实现注册界面.

  • 在AppDelegate中实现的方法
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    
    //程序启动
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        
        //初始化window
        let frame = UIScreen.main.bounds
        self.window = UIWindow(frame: frame)
        
        //初始化控制器
        let MyVC = MyViewController()
        
        //设置成window的根视图控制器
        self.window?.rootViewController = MyVC
        
        //把window设置成系统的主window
        self.window?.makeKeyAndVisible()
        return true
    }


  • 按住"command + n"新建一个Cocoa Touch Class,将其命名为MyViewController,实现的方法如下:
import UIKit
class MyViewController: UIViewController ,UITextFieldDelegate{
   override func viewDidLoad() {
       super.viewDidLoad()
       self.view.backgroundColor = #colorLiteral(red: 0.9098039269, green: 0.4784313738, blue: 0.6431372762, alpha: 1)
       
       let imageview = UIImageView(frame: CGRect(x: 100, y: 30, width: 100, height: 100))
       imageview.backgroundColor = UIColor.blue
       imageview.image = UIImage(named: "51.jpg")
       imageview.layer.cornerRadius = 50
       imageview.layer.masksToBounds = true
       self.view.addSubview(imageview)
       
       let label1 = UILabel(frame: CGRect(x: 30, y: 240, width: 60, height: 40))
       label1.backgroundColor = UIColor.cyan
       label1.text = "用户名"
       label1.textAlignment = .center
       label1.layer.cornerRadius = 8
       label1.layer.masksToBounds = true
       label1.isUserInteractionEnabled = true
       self.view.addSubview(label1)
       
       let textfield1 = UITextField(frame: CGRect(x: 100, y: 240, width: 200, height: 40))
       textfield1.backgroundColor = UIColor.cyan
       textfield1.borderStyle = .roundedRect
       textfield1.clearButtonMode = .whileEditing
       textfield1.clearsOnBeginEditing = true
       textfield1.placeholder = "请输入用户名"
       textfield1.delegate = self
       textfield1.tag = 10001
       self.view.addSubview(textfield1)
       
       
       let label2 = UILabel(frame: CGRect(x: 30, y: 300, width: 60, height: 40))
       label2.backgroundColor = UIColor.cyan
       label2.text = "密  码"
       label2.textAlignment = .center
       label2.layer.cornerRadius = 8
       label2.layer.masksToBounds = true
       label2.isUserInteractionEnabled = true
       self.view.addSubview(label2)
      
       
       let textfield2 = UITextField(frame: CGRect(x: 100, y: 300, width: 200, height: 40))
       textfield2.backgroundColor = UIColor.cyan
       textfield2.borderStyle = .roundedRect
       textfield2.clearButtonMode = .whileEditing
       textfield2.clearsOnBeginEditing = true
       textfield2.placeholder = "请输入密码"
       textfield2.delegate = self
       textfield2.tag = 10002
       self.view.addSubview(textfield2)
       
       
       let button1 = UIButton(type: .system)
       button1.frame = CGRect(x: 70, y: 380, width: 80, height: 50)
       button1.backgroundColor = UIColor.yellow
       button1.setTitle("登录", for: .normal)
       button1.layer.cornerRadius = 8
       button1.layer.masksToBounds = true
       button1.addTarget(self, action: #selector(button1Action(button1:)), for: .touchUpInside)
       self.view.addSubview(button1)
       
       let button2 = UIButton(type: .system)
       button2.frame = CGRect(x: 200, y: 380, width: 80, height: 50)
       button2.backgroundColor = UIColor.yellow
       button2.setTitle("注册", for: .normal)
       button2.layer.cornerRadius = 8
       button2.layer.masksToBounds = true
       button2.addTarget(self, action: #selector(button2Action(button2:)), for: .touchUpInside)
       self.view.addSubview(button2)
   }
   func button1Action(button1:UIButton){
       print("登录成功")
   }
   func button2Action(button2:UIButton){
       print("注册成功")
   }
   override func didReceiveMemoryWarning() {
       super.didReceiveMemoryWarning()
       // Dispose of any resources that can be recreated.
   }

效果图展示:


注册界面小Demo_第1张图片
简单注册界面.png

你可能感兴趣的:(注册界面小Demo)