当我编辑textfield textField(_:shouldChangeCharactersInRange:replacementString :)时没有被调用

时间:2016-06-20 08:16:12

标签: swift

我正在为ipad创建自定义键盘,当我按下键盘按钮并编辑文本字段时textField(_:shouldChangeCharactersInRange:replacementString:)无法调用

import UIKit


class KeyBoardView: UIView{

enum KeyBoardType {

    case AlphaNumeric
    case Alpha
    case Numeric
}

var textField: CustomKeyboardTextField!

var textInput: UITextInput?{
    get{
        return self.textField
    }

}

var keyboardView: KeyBoardView!

let win: UIWindow = (((UIApplication.sharedApplication().delegate?.window)!)!)

let alphaKeyBoardView = KeyBoardAlphaView.instanceFromNib()
let numKeyBoardView = KeyBoardNumericView.instanceFromNib()

class func instanceFromNib() -> KeyBoardView{
   let win: UIWindow = (((UIApplication.sharedApplication().delegate?.window)!)!)
     let arrView = win.subviews
    for view in arrView {

        if view.isKindOfClass(KeyBoardView) {
            view.removeFromSuperview()
            break
        }
    }

    return  KeyBoardView(frame: CGRect(x: 0, y: win.bounds.size.height*0.6, width: win.bounds.size.width, height: win.bounds.size.height*0.4))
}


func keyboardTypeSet(notification: AnyObject) {

    keyboardView = notification.object as! KeyBoardView
    self.textField = keyboardView.textField

    let arrView = win.subviews
    var isPresentKView: Bool = false
    for view in arrView {

        if view.isKindOfClass(KeyBoardView) {

            let arrSubView = view.subviews
            for subView in arrSubView {

                subView.removeFromSuperview()
            }
            isPresentKView = true
            setkType(textField.kType)
            break
        }
    }


    if !isPresentKView {
        setkType(textField.kType)

        keyboardView.backgroundColor = UIColor.clearColor()
        //                setkType(textField.kType)
        keyboardView.translatesAutoresizingMaskIntoConstraints = false
        win.addSubview(keyboardView)

        let leadingConstraint = NSLayoutConstraint(item: keyboardView, attribute: .Leading, relatedBy: .Equal, toItem: win, attribute: .Leading, multiplier: 1, constant: 0)
        let trailingConstraint = NSLayoutConstraint(item: keyboardView, attribute: .Trailing, relatedBy: .Equal, toItem: win, attribute: .Trailing, multiplier: 1, constant:  0)
        let bottomConstraint = NSLayoutConstraint(item: keyboardView, attribute: .Bottom, relatedBy: .Equal, toItem: win, attribute: .Bottom, multiplier: 1, constant: 0)
        let heightConstraint = NSLayoutConstraint(item: keyboardView, attribute: .Height, relatedBy: .Equal, toItem: win, attribute: .Height, multiplier: 0.4, constant:  0)
        win.addConstraints([leadingConstraint,trailingConstraint,bottomConstraint,heightConstraint])

        keyboardView.layoutIfNeeded()
        win.layoutIfNeeded()

    }


}

func setkType(keyboardType: KeyBoardType) {

    switch keyboardType {

    case .AlphaNumeric:
        alphaNumericKeyboard()

    case .Alpha:
        alphaKeyboard()

    case .Numeric:
        numericKeyboard()

    }

}

func alphaNumericKeyboard() {

    keyboardView.addSubview(alphaKeyBoardView)
    keyboardView.addSubview(numKeyBoardView)

    alphaKeyBoardView.translatesAutoresizingMaskIntoConstraints = false
    numKeyBoardView.translatesAutoresizingMaskIntoConstraints = false


    let alLeadingConstraint = NSLayoutConstraint(item: alphaKeyBoardView, attribute: .Leading, relatedBy: .Equal, toItem: keyboardView, attribute: .Leading, multiplier: 1, constant: 0)
    let alBottomConstraint = NSLayoutConstraint(item: alphaKeyBoardView, attribute: .Bottom, relatedBy: .Equal, toItem: keyboardView, attribute: .Bottom, multiplier: 1, constant: 0)
    let alTopConstraint = NSLayoutConstraint(item: alphaKeyBoardView, attribute: .Top, relatedBy: .Equal, toItem: keyboardView, attribute: .Top, multiplier: 1, constant:  0)
    let alWidthConstraint = NSLayoutConstraint(item: alphaKeyBoardView, attribute: .Width, relatedBy: .Equal, toItem: keyboardView, attribute: .Width, multiplier: 0.7, constant:  0)

    //            let horizantalConstraint = NSLayoutConstraint(item: alphaKeyBoardView, attribute: .Right, relatedBy: .Equal, toItem: numKeyBoardView, attribute: .Left, multiplier: 1, constant: 0)

    let numTrailingConstraint = NSLayoutConstraint(item: numKeyBoardView, attribute: .Trailing, relatedBy: .Equal, toItem: keyboardView, attribute: .Trailing, multiplier: 1, constant: 0)
    let numBottomConstraint = NSLayoutConstraint(item: numKeyBoardView, attribute: .Bottom, relatedBy: .Equal, toItem: keyboardView, attribute: .Bottom, multiplier: 1, constant: 0)
    let numLeadingConstraint = NSLayoutConstraint(item: numKeyBoardView, attribute: .Leading, relatedBy: .Equal, toItem: alphaKeyBoardView, attribute: .Trailing, multiplier: 1, constant: 0)
    let numHeightConstraint = NSLayoutConstraint(item: numKeyBoardView, attribute: .Height, relatedBy: .Equal, toItem: keyboardView, attribute: .Height, multiplier: 1, constant: 0)

    keyboardView.addConstraints([alLeadingConstraint,alBottomConstraint,alTopConstraint,alWidthConstraint,numBottomConstraint,numLeadingConstraint,numTrailingConstraint,numHeightConstraint])
}

func alphaKeyboard(){
    alphaKeyBoardView.translatesAutoresizingMaskIntoConstraints = false

    keyboardView.addSubview(alphaKeyBoardView)

    let alLeadingConstraint = NSLayoutConstraint(item: alphaKeyBoardView, attribute: .Leading, relatedBy: .Equal, toItem: keyboardView, attribute: .Leading, multiplier: 1, constant: 0)
    let alTrailingConstraint = NSLayoutConstraint(item: alphaKeyBoardView, attribute: .Trailing, relatedBy: .Equal, toItem: keyboardView, attribute: .Trailing, multiplier: 1, constant: 0)
    let alTopConstriant = NSLayoutConstraint(item: alphaKeyBoardView, attribute: .Top, relatedBy: .Equal, toItem: keyboardView, attribute: .Top, multiplier: 1, constant: 0)
    let alBottomConstraint = NSLayoutConstraint(item: alphaKeyBoardView, attribute: .Bottom, relatedBy: .Equal, toItem: keyboardView, attribute: .Bottom, multiplier: 1, constant: 0)

    keyboardView.addConstraints([alLeadingConstraint,alTrailingConstraint,alTopConstriant,alBottomConstraint])

}

func numericKeyboard(){
    numKeyBoardView.translatesAutoresizingMaskIntoConstraints = false

//        keyboardView.frame = CGRect(x: 0, y: window!.bounds.size.height*0.6, width: window!.bounds.size.width*0.3, height: window!.bounds.height*0.4)

    keyboardView.addSubview(numKeyBoardView)

    let numLeadingConstraint = NSLayoutConstraint(item: numKeyBoardView, attribute: .Leading, relatedBy: .Equal, toItem: keyboardView, attribute: .Leading, multiplier: 1, constant: 0)
    let numTopConstraint = NSLayoutConstraint(item: numKeyBoardView, attribute: .Top, relatedBy: .Equal, toItem: keyboardView, attribute: .Top, multiplier: 1, constant: 0)
    let numWidthConstraint = NSLayoutConstraint(item: numKeyBoardView, attribute: .Width, relatedBy: .Equal, toItem: keyboardView, attribute: .Width, multiplier: 0.3, constant: 0)
    let numBottomConstraint = NSLayoutConstraint(item: numKeyBoardView, attribute: .Bottom, relatedBy: .Equal, toItem: keyboardView, attribute: .Bottom, multiplier: 1, constant: 0)

    //            let numHeightConstraint = NSLayoutConstraint(item: numKeyBoardView, attribute: .Height, relatedBy: .Equal, toItem: keyboardView, attribute: .Height, multiplier: 1, constant: 0)

    keyboardView.addConstraints([numLeadingConstraint,numTopConstraint,numBottomConstraint,numWidthConstraint])

}


}

1 个答案:

答案 0 :(得分:0)

添加

textField.delegate = self 

将UITextFieldDelegate添加到类

class KeyBoardView: UIView,UITextFieldDelegate {