ios-swift 中自定义控件的实现

先讲一下思路

我们继承UIView写一个LTView,用UILabel和UITextField作为LTView的子View.

1.首先创建一个新文件,选择xcode菜单,File -> New -> File


ios-swift 中自定义控件的实现_第1张图片


ios-swift 中自定义控件的实现_第2张图片

2.得到文件LTView.swift,我们给LTView创建两个属性,并对其初始化法

ios-swift 中自定义控件的实现_第3张图片


ios-swift 中自定义控件的实现_第4张图片

3.这时会出现一个系统能够自动修改的 错误,所以我们利用系统的错误修正功能对其自行修正,会自动补上下列代码

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)

//设置文字居中

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

//设置密闻输入

self.textField.isSecureTextEntry = true

self.textField.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)

self.addSubview(textField)

}

4.回到 初始化方法中,对新定义的setupSubView方法进行调用

override init(frame: CGRect) {

//调用父类对这个方法的实现

super.init(frame: frame)

self.setupSubView() //调用子视图方法

}

5.进入AppDelegate.swift中

class AppDelegate: UIResponder, UIApplicationDelegate ,UITextFieldDelegate{

//UITextFieldDelegate代理需要遵守的协议

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 = "请输入密码"

//设置代理

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 {       

 //第一响应事件       

 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) {

}

}

最终效果

ios-swift 中自定义控件的实现_第5张图片

你可能感兴趣的:(ios-swift 中自定义控件的实现)