swift_界面
注冊界面
- 先定義一個image,并設(shè)置frame,在添加到父視圖上
<pre>
let image = UIImageView(frame: CGRect(x: 140, y: 70, width: 120, height: 100))
self.view.addSubview(image)
image.image = UIImage(named: "人.png")
</pre>
- 然后再定義一個為用戶名的TextField1,初始化并設(shè)置frame,添加到父視圖
<pre>
self.textField1 = UITextField()
textField1.frame = CGRect(x: 50, y: 200, width: 300, height: 40)
self.view.addSubview(textField1)
</pre>
** 設(shè)置textField1的placeholder(輸入框的默認(rèn)值)
<pre>
textField1.placeholder = "請輸入用戶名"
</pre>
** 設(shè)置textField1的邊框樣式
<pre>
//圓角邊框
textField1.borderStyle = .roundedRect
</pre>
** 設(shè)置textField1的復(fù)選框
<pre>
textField1.leftView = UIImageView(image: UIImage(named: "用戶.png"))
textField1.leftViewMode = .always
</pre>
** 設(shè)置textField1內(nèi)部最后面的的刪除小圓點
<pre>
textField1.clearButtonMode = .whileEditing
</pre>
我們需要再次定義兩個textField余佛,分別為密碼的textField2和再次輸入密碼的textField3,和textField1的內(nèi)容一樣就漾,如以下代碼:
<pre>
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
</pre>
- 定義一個button按鈕,設(shè)置其內(nèi)容
<pre>
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)
</pre>
- 給button設(shè)置一個點擊事件
<pre>
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
}
}
}
</pre>
- 顯示結(jié)果:
感謝大家觀看!!茄螃!??雖然界面不美觀,但是可以提供初學(xué)者的學(xué)習(xí)慈俯。??
作者:石頭