美文网首页 Swift3.0iOS Developer
ios-swift 中自定义控件的实现

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

作者: 七叶5 | 来源:发表于2016-11-21 20:36 被阅读659次

先讲一下思路

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

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

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

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 中自定义控件的实现

    先讲一下思路 我们继承UIView写一个LTView,用UILabel和UITextField作为LTView的子...

  • Android中的自定义控件

    Android中的自定义控件大致可以分成三类:自定义组合控件、继承原生控件的自定义控件、继承View自己实现绘制的...

  • 关于canvas那些事儿

    实现自定义控件,无非分成这么几步: Ⅰ、在OnMeasure()方法中,测量自定义控件的大小,使自定义控件能够自适...

  • 自定义view实现粘性拉动效果

    Chapter 2 2-1 添加自定义控件并绘制圆 新建一个自定义控件 实现基本初始化方法 在自定义控件中绘制圆代...

  • Quartz2D(一)之简单介绍

    Quarz 2D 一. 自定义一个UI控件的样式 直接在该自定义控件的类中实现- (void)drawRect:(...

  • 自定义组合控件,事件传递响应规则

    自定义组合控件 当系统提供的控件,不足以满足我们开发需求时,可以通过自定义控件来实现更好的效果。 组合控件的实现步...

  • iOS自定义控件开发梳理

    在日常iOS开发中,系统提供的控件常常无法满足业务功能,这个时候需要我们实现一些自定义控件。自定义控件能让我们完全...

  • iOS 自定义控件 自定义进度条

    在日常iOS开发中,系统提供的控件常常无法满足业务功能,这个时候需要我们实现一些自定义控件。自定义控件能让我们完全...

  • iOS-Swift-贴边框小机器人展示

    之前有写过一个仿锤子录屏控件的拖拽demo,iOS-Swift使用ReplayKit实现录屏功能,我想起以前我还在...

  • 自定义控件 - Spinner

    blog 中会记录一些自定义控件的实现过程,完整的使用和其他的自定义控件可以参考,常用控件库WidgetProdu...

网友评论

    本文标题:ios-swift 中自定义控件的实现

    本文链接:https://www.haomeiwen.com/subject/nbgupttx.html