美文网首页
iOS-自定义键盘

iOS-自定义键盘

作者: Swift从入门到崩溃 | 来源:发表于2016-08-15 23:22 被阅读0次

声明协议

import UIKit
protocol LYMKeyBoardDelegate {
func showButtonTitle(buttonTitle:String)
}

class文件

class LYMKeyBoard: UIView {
//代理
var delegate:LYMKeyBoardDelegate?

//自定义控件
//1.继承uiview创建新的类
//2.确定自定义控件上的子视图
//3.在构造方法中单纯的创建子视图(不去设置feame相关属性)
//4.在layoutSubviews()设置子视图frame
override init(frame: CGRect) {
    super.init(frame: frame)
    //确定每个按钮上要显示的内容
    let contents = ["1","2","3","4","5","6","7","8","9","0","X","返回"]
    // 创建显示内容所对应的按钮
    for item in contents {
        //创建按钮
        let button = UIButton()
        //设置文字
        button.setTitle(item, forState: .Normal)
        button.setImage(UIImage.init(named: "cry"), forState: .Normal)
        button.addTarget(self, action: #selector(btnAction(_:)), forControlEvents: .TouchUpInside)
        button.layer.borderWidth = 2
        button.layer.borderColor = UIColor.blackColor().CGColor
        self.addSubview(button)
    }
}

调用协议

func btnAction(btn:UIButton){
//按钮点击之后想要将按钮的内容显示在textfeild上面,但是当前keyboard做不到。想要viewController来帮他做。->代理模式
//三要素
//委托:LYMKeyBoard
//协议:想要将按钮的内容显示在textfeild上面
//代理:viewController

//按钮点击的时候让按钮显示按钮的title
self.delegate?.showButtonTitle(btn.currentTitle!)
    
    }
    

//只有在视图中创建或重写了构造方法,那么就必须实现下面的这个方法
required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

这个方法只有在视图真正的显示在界面上的时候才会第一次调用

override func layoutSubviews() {
    //公共属性:
    //间距
    let margin:CGFloat = 15
    //keyBoard的宽=屏幕的宽
    let keyWidth = UIScreen.mainScreen().bounds.size.width
    let keyHeight = self.frame.size.height
    //按钮的宽度和高度,
    let width = (keyWidth - 4*margin)/3
    let height = (keyHeight - margin*5)/4
    var i = 0
    for item in self.subviews{
         //保存当前按钮对应的位置
        //判段一个对象的类型是否是指定的类型
        if item.isKindOfClass(UIButton.self){
            //强制转换(将UIView强者转换成UIButton),前提是被转换的对象的实质就是被转换的类型
            let button = item as! UIButton
            //计算按钮的frame
            let x = (margin+width)*CGFloat(i%3) + margin
            let y = (margin+height)*CGFloat(i/3) + margin
            
            button.frame = CGRectMake(x, y, width, height)
            i += 1
        
        }else{
            print("不是按钮")
        }
    }
}
}

2.调用自定义键盘

import UIKit

class ViewController: UIViewController {
//MARK:生命周期
 let textFeild = UITextField()
override func viewDidLoad() {
    super.viewDidLoad()
    textFeild.frame = CGRectMake(100, 100, 250, 50)
    textFeild.backgroundColor = UIColor.yellowColor()
    textFeild.background = UIImage.init(named: "cry")
    self.view.addSubview(textFeild)
    
    //创建自己的键盘
    let keyBoard = LYMKeyBoard.init(frame: CGRectMake(0, 0, 0, 256))
    //设置代理
    keyBoard.delegate = self
    keyBoard.backgroundColor = UIColor.greenColor()
    textFeild.inputView = keyBoard
    
    //切原角(所有视图都可以)
    //每个视图都有一个layer,专门负责视图的显示
    //让layer可以裁剪
    textFeild.layer.masksToBounds = true
    //设置圆角半径
    textFeild.layer.cornerRadius = 30
    //加边框
    //设置边框的宽度
    textFeild.layer.borderWidth = 3
    //设置边框的颜色
    textFeild.layer.borderColor = UIColor.brownColor().CGColor
    


}

}

遵守协议方法

extension ViewController:LYMKeyBoardDelegate{
func showButtonTitle(buttonTitle: String) {
    //获取到textfeild原来的内容
    var textStr = ""
    if let str = textFeild.text{
        textStr = str
    }
    //如果按钮点击的是”返回“
    if buttonTitle == "返回"{
        textFeild.resignFirstResponder()
        //键盘收起之后,当前函数直接结束
        return
    }
    if buttonTitle == "X"{
        //判断textfeild是否为空?
        if textFeild.text?.isEmpty == false {
        let index = textFeild.text?.endIndex.predecessor()
       textStr.removeAtIndex(index!)
        //将删除后的内容在赋值给textfield
            textFeild.text = textStr
        }
            return
    }

    //如果没有点击返回和删除就将点击的内容拼接到原来的内容上
    textFeild.text = textStr+buttonTitle
}
}

相关文章

网友评论

      本文标题:iOS-自定义键盘

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