美丽的代码~~~

swift_界面

注册界面

  • 先定义一个image,并设置frame,在添加到父视图上


let image = UIImageView(frame: CGRect(x: 140, y: 70, width: 120, height: 100))
self.view.addSubview(image)
image.image = UIImage(named: "人.png")

  • 然后再定义一个为用户名的TextField1,初始化并设置frame,添加到父视图


self.textField1 = UITextField()
textField1.frame = CGRect(x: 50, y: 200, width: 300, height: 40)
self.view.addSubview(textField1)

** 设置textField1的placeholder(输入框的默认值)


textField1.placeholder = "请输入用户名"

** 设置textField1的边框样式


//圆角边框
textField1.borderStyle = .roundedRect

** 设置textField1的复选框


textField1.leftView = UIImageView(image: UIImage(named: "用户.png"))
textField1.leftViewMode = .always

** 设置textField1内部最后面的的删除小圆点


textField1.clearButtonMode = .whileEditing

我们需要再次定义两个textField,分别为密码的textField2和再次输入密码的textField3,和textField1的内容一样,如以下代码:


self.textField2 = UITextField()
textField2.frame = CGRect(x: 50, y: 240, width: 300, height: 40)
self.view.addSubview(textField2)
textField2.placeholder = "请输入密码"
textField2.borderStyle = .roundedRect
textField2.leftView = UIImageView(image: UIImage(named: "锁子.png"))
textField2.clearButtonMode = .whileEditing
textField2.leftViewMode = .always
textField2.isSecureTextEntry = true
let textField3 = UITextField()
textField3.frame = CGRect(x: 50, y: 280, width: 300, height: 40)
self.view.addSubview(textField3)
textField3.placeholder = "请再次输入密码"
textField3.borderStyle = .roundedRect
textField3.leftView = UIImageView(image: UIImage(named: "锁子.png"))
textField3.clearButtonMode = .whileEditing
textField3.leftViewMode = .always
textField3.isSecureTextEntry = true

  • 定义一个button按钮,设置其内容


let btn = UIButton(frame: CGRect(x: 50, y: 340, width: 300, height: 40))
self.view.addSubview(btn)
btn.backgroundColor = UIColor.blue
btn.setTitle("注册", for: .normal)
btn.addTarget(self, action: #selector(btnAction(btn:)), for: .touchUpInside)

  • 给button设置一个点击事件


func btnAction(btn:UIButton) {
if let _ = self.textField1.text {
if (self.textField1.text?.characters.count)! < 6{
self.textField1.text="用户名不能低于6位"
return
}
if (self.textField1.text?.characters.count)! > 13{
self.textField1.text="用户名不能高于13位"
return
}
if self.textField1.text == self.textField2.text{
self.textField2.text="用户名和密码不能相同"
return
}
}
}

  • 显示结果:
美丽的代码~~~_第1张图片

感谢大家观看!!!虽然界面不美观,但是可以提供初学者的学习。

作者:石头

你可能感兴趣的:(美丽的代码~~~)