先講一下思路
我們繼承UIView寫一個LTView,用UILabel和UITextField作為LTView的子View.
1.首先創(chuàng)建一個新文件,選擇xcode菜單,F(xiàn)ile -> New -> File
2.得到文件LTView.swift,我們給LTView創(chuàng)建兩個屬性,并對其初始化法
3.這時會出現(xiàn)一個系統(tǒng)能夠自動修改的 錯誤,所以我們利用系統(tǒng)的錯誤修正功能對其自行修正,會自動補上下列代碼
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
4.下面我們寫布局一個子視圖的方法setupSubView,初始化label和textField
func setupSubView(){
//初始化這兩個屬性label和textField
self.label = UILabel(frame: CGRect(x: 0, y: 0, width: self.frame.size.width*0.2, height: self.frame.size.height))
self.label.backgroundColor = #colorLiteral(red: 0.8039215803, green: 0.8039215803, blue: 0.8039215803, alpha: 1)
//設(shè)置文字居中
self.label.textAlignment = .center
//切圓角
self.label.layer.cornerRadius = 5
self.label.clipsToBounds = true
self.addSubview(label)//添加到LTView上
self.textField = UITextField(frame: CGRect(x: self.frame.size.width*0.2+10, y: 0, width: self.frame.size.width*0.8-10, height: self.frame.size.height))
//定義圓角模式
self.textField.borderStyle = .roundedRect
//設(shè)置密聞輸入
self.textField.isSecureTextEntry = true
self.textField.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
self.addSubview(textField)
}
4.回到 初始化方法中,對新定義的setupSubView方法進行調(diào)用
override init(frame: CGRect) {
//調(diào)用父類對這個方法的實現(xiàn)
super.init(frame: frame)
self.setupSubView() //調(diào)用子視圖方法
}
5.進入AppDelegate.swift中
class AppDelegate: UIResponder, UIApplicationDelegate ,UITextFieldDelegate{
//UITextFieldDelegate代理需要遵守的協(xié)議
var window: UIWindow?
//定義為全局的變量
var textField:UITextField!
var textField2:UITextField!
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.backgroundColor = #colorLiteral(red: 0.9098039269, green: 0.4784313738, blue: 0.6431372762, alpha: 1)
self.window?.makeKeyAndVisible()
self.window?.rootViewController = ViewController()
let image = UIImageView(image: UIImage(named: "girl2.jpg"))
image.frame = CGRect(x: 100, y: 50, width: 200, height: 200)
//對圖片進行切圓角處理
image.layer.cornerRadius = 100;
image.clipsToBounds = true
self.window?.addSubview(image)
let aLTView = LTView(frame: CGRect(x: 7, y: 300, width: 400, height: 50))
aLTView.label.text = "用戶名"
//文本對齊格式,textAlignment是一個枚舉,所以可以省略類型名
//aLTView.label.textAlignment = NSTextAlignment.center
aLTView.label.textAlignment = .center
aLTView.textField.placeholder = "請輸入用戶名"
aLTView.textField.delegate = self
self.window?.addSubview(aLTView)
let bLTView = LTView(frame: CGRect(x: 7, y: 370, width: 400, height: 50))
bLTView.label.text = "密碼"
//提示字符
bLTView.textField.placeholder = "請輸入密碼"
//設(shè)置代理
bLTView.textField.delegate = self
self.window?.addSubview(bLTView)
let registerButton = UIButton(frame: CGRect(x: 80, y: 500, width: 100, height: 50))
registerButton.backgroundColor = UIColor.gray
registerButton.setTitle("登錄", for: .normal)
self.window?.addSubview(registerButton)
let cancelButton = UIButton(frame: CGRect(x: 220, y: 500, width: 100, height: 50))
cancelButton.backgroundColor = UIColor.gray
cancelButton.setTitle("取消", for: .normal)
self.window?.addSubview(cancelButton)
textField = aLTView.textField
textField2 = bLTView.textField
return true
}
//點擊return收起鍵盤? ??
func textFieldShouldReturn(_ textField: UITextField) -> Bool {? ? ? ?
?//第一響應(yīng)事件? ? ? ?
?textField.resignFirstResponder()? ? ??
?return true? ? } ? ?
//點擊空白處收起鍵盤? ??
override func touchesBegan(_ touches: Set, with event: UIEvent?) {
textField.resignFirstResponder()
textField2.resignFirstResponder()
}
func applicationWillResignActive(_ application: UIApplication) {
}
func applicationDidEnterBackground(_ application: UIApplication) {
}
func applicationWillEnterForeground(_ application: UIApplication) {
}
func applicationDidBecomeActive(_ application: UIApplication) {
}
func applicationWillTerminate(_ application: UIApplication) {
}
}
最終效果