键盘将显示通知。不给出键盘出现的通知

时间:2019-05-02 22:53:09

标签: ios swift custom-keyboard

我的viewDidLoad()中自定义键盘扩展名具有以下通知设置:

    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)

这是我在viewDidLoad()之外调用的函数:

@objc func keyboardWillShow(_ notification: NSNotification) {
    if let keyboardRect = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
        print(keyboardRect.height)
        ScreenHeight = Int(keyboardRect.height)
    }
}

但是,我在keyboardWillShow()函数中设置了一个断点,但从未触发过该断点。在显示键盘之前,我还需要做些其他事情来获得键盘的大小吗?

我试图动态调整以编程方式插入键盘的元素的大小。

让我知道是否需要更多信息,或者这是否很明显。

完整代码:

class KeyboardViewController: UIInputViewController {


@IBOutlet var nextKeyboardButton: UIButton! = UIButton(type: .system)

var centralManager: CBCentralManager!
var NUB5: CBPeripheral!
var characteristics = [CBCharacteristic]()
var connection: UILabel = UILabel()
var serialNum: UILabel = UILabel()
var serial: String = ""
var ScreenHeight = 0
let cornerRadii = 4

//setup Number buttons
var num1: UIButton = UIButton()
var num2: UIButton = UIButton()
var num3: UIButton = UIButton()
var num4: UIButton = UIButton()
var num5: UIButton = UIButton()
var num6: UIButton = UIButton()
var num7: UIButton = UIButton()
var num8: UIButton = UIButton()
var num9: UIButton = UIButton()
var num0: UIButton = UIButton()
var spaceBar: UIButton = UIButton()
var plus: UIButton = UIButton()
var minus: UIButton = UIButton()
var back: UIButton = UIButton()
var left: UIButton = UIButton()
var right: UIButton = UIButton()
var period: UIButton = UIButton()
var comma: UIButton = UIButton()
var returnKey: UIButton = UIButton()


let screensize: CGRect = UIScreen.main.bounds


override func updateViewConstraints() {
    super.updateViewConstraints()

    // Add custom view sizing constraints here
}

override func viewDidLoad() {
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(_:)), name: UIResponder.keyboardDidChangeFrameNotification, object: nil)

    var keyboard: UIView!

    func loadInterface() {
        // load the nib file
        var calculatorNib = UINib(nibName: "keyboard", bundle: nil)
        // instantiate the view
        keyboard = (calculatorNib.instantiate(withOwner: self, options: nil)[0] as! UIView)
        // add the interface to the main view
        view.addSubview(keyboard)
        // copy the background color
        view.backgroundColor = keyboard.backgroundColor
        ScreenHeight = Int(self.accessibilityFrame.height)
        print(ScreenHeight)
    }
    super.viewDidLoad()


    loadInterface()

    self.centralManager = CBCentralManager(delegate: self, queue: nil)     



    //Setup Labels
    let width = screensize.width
    let height = CGFloat(200) //subtract 10 from total screensize to give space for connection indicator
    let screensize = self.view.bounds.size
    //ScreenHeight = Int(height)
    connection.frame = CGRect(x: 0,y: 0,width: width,height: 10)
    //TODO
    serialNum.frame = CGRect(x: width/2+5, y:10,width: width/2 - 10,height:15)

    spaceBar.frame = CGRect(x: width*4/5+4, y: height/4+10, width: width/5, height: height/4)
    num1.frame = CGRect(x:width/5,y: 10, width: width/5,height: height/4)
    num2.frame = CGRect(x:width*2/5,y: 10, width: width/5,height: height/4)
    num3.frame = CGRect(x:width*3/5,y: 10, width: width/5,height: height/4)
    num4.frame = CGRect(x:width/5+1,y: height/4+10, width: width/5,height: height/4)
    num5.frame = CGRect(x:width*2/5+2,y: height/4+10, width: width/5,height: height/4)
    num6.frame = CGRect(x:width*3/5+3,y: height/4+10, width: width/5,height: height/4)
    num7.frame = CGRect(x:width/5+1,y: height/2+10, width: width/5,height: height/4)
    num8.frame = CGRect(x:width*2/5+2,y: height/2+10, width: width/5,height: height/4)
    num9.frame = CGRect(x:width*3/5+3,y: height/2+10, width: width/5,height: height/4)
    num0.frame = CGRect(x:width*2/5+3,y: 3*height/4+10, width: width/5,height: height/4)
    plus.frame = CGRect(x:0,y: 10, width: width/5,height: height/4)
    minus.frame = CGRect(x:0,y: height/4+10, width: width/5,height: height/4)
    back.frame = CGRect(x:width*4/5,y:10,width:width/5,height:height/4)
    right.frame = CGRect(x: width*4/5+4, y: height/2+10, width: width/5, height: height/4)
    left.frame = CGRect(x:0,y: height/2+10, width: width/5,height: height/4)
    nextKeyboardButton.frame = CGRect(x:0,y: 3*height/4+10, width: width/5,height: height/4)
    period.frame = CGRect(x:width/5,y: 3*height/4+10, width: width/5,height: height/4)
    comma.frame = CGRect(x:width*3/5+2,y: 3*height/4+10, width: width/5,height: height/4)
    returnKey.frame = CGRect(x:width*4/5+3,y: height*3/4+10, width: width/5,height: height/4)

    num1.addTarget(self,action: #selector(addOne), for: .touchUpInside)
    num2.addTarget(self,action: #selector(addTwo), for: .touchUpInside)
    num3.addTarget(self,action: #selector(addThree), for: .touchUpInside)
    num4.addTarget(self,action: #selector(addFour), for: .touchUpInside)
    num5.addTarget(self,action: #selector(addFive), for: .touchUpInside)
    num6.addTarget(self,action: #selector(addSix), for: .touchUpInside)
    num7.addTarget(self,action: #selector(addSeven), for: .touchUpInside)
    num8.addTarget(self,action: #selector(addEight), for: .touchUpInside)
    num9.addTarget(self,action: #selector(addNine), for: .touchUpInside)
    num0.addTarget(self,action: #selector(addZero), for: .touchUpInside)
    spaceBar.addTarget(self,action: #selector(space), for: .touchUpInside)
    plus.addTarget(self, action: #selector(plusSign), for: .touchUpInside)
    minus.addTarget(self, action: #selector(minusSign), for: .touchUpInside)
    back.addTarget(self, action: #selector(deleteOne), for: .touchUpInside)
    left.addTarget(self, action: #selector(leftOne), for: .touchUpInside)
    right.addTarget(self, action: #selector(rightOne), for: .touchUpInside)
    nextKeyboardButton.addTarget(self, action: #selector(handleInputModeList(from:with:)), for: .allTouchEvents)
    period.addTarget(self, action: #selector(addPeriod), for: .touchUpInside)
    comma.addTarget(self, action: #selector(addComma), for: .touchUpInside)
    returnKey.addTarget(self, action: #selector(returnButton), for: .touchUpInside)

    num1.setTitle("1", for: .normal)
    num2.setTitle("2", for: .normal)
    num3.setTitle("3", for: .normal)
    num4.setTitle("4", for: .normal)
    num5.setTitle("5", for: .normal)
    num6.setTitle("6", for: .normal)
    num7.setTitle("7", for: .normal)
    num8.setTitle("8", for: .normal)
    num9.setTitle("9", for: .normal)
    num0.setTitle("0", for: .normal)
    spaceBar.setTitle("SPACE", for: .normal)
    back.setTitle("<-", for: .normal)
    plus.setTitle("+", for: .normal)
    minus.setTitle("-", for: .normal)
    left.setTitle("<", for: .normal)
    right.setTitle(">", for: .normal)
    nextKeyboardButton.setTitle("NEXT", for: .normal)
    period.setTitle(".", for: .normal)
    comma.setTitle(",", for: .normal)
    returnKey.setTitle("Return", for: .normal)

    num1.layer.cornerRadius = CGFloat(cornerRadii)
    num2.layer.cornerRadius = CGFloat(cornerRadii)
    num3.layer.cornerRadius = CGFloat(cornerRadii)
    num4.layer.cornerRadius = CGFloat(cornerRadii)
    num5.layer.cornerRadius = CGFloat(cornerRadii)
    num6.layer.cornerRadius = CGFloat(cornerRadii)
    num7.layer.cornerRadius = CGFloat(cornerRadii)
    num8.layer.cornerRadius = CGFloat(cornerRadii)
    num9.layer.cornerRadius = CGFloat(cornerRadii)
    num0.layer.cornerRadius = CGFloat(cornerRadii)
    spaceBar.layer.cornerRadius = CGFloat(cornerRadii)
    plus.layer.cornerRadius = CGFloat(cornerRadii)
    minus.layer.cornerRadius = CGFloat(cornerRadii)
    back.layer.cornerRadius = CGFloat(cornerRadii)
    left.layer.cornerRadius = CGFloat(cornerRadii)
    right.layer.cornerRadius = CGFloat(cornerRadii)
    nextKeyboardButton.layer.cornerRadius = CGFloat(cornerRadii)
    period.layer.cornerRadius = CGFloat(cornerRadii)
    comma.layer.cornerRadius = CGFloat(cornerRadii)
    returnKey.layer.cornerRadius = CGFloat(cornerRadii)

    num5.layer.borderWidth = CGFloat(1)


    num1.setTitleColor(UIColor.black, for: .normal)
    num2.setTitleColor(UIColor.black, for: .normal)
    num3.setTitleColor(UIColor.black, for: .normal)
    num4.setTitleColor(UIColor.black, for: .normal)
    num5.setTitleColor(UIColor.black, for: .normal)
    num6.setTitleColor(UIColor.black, for: .normal)
    num7.setTitleColor(UIColor.black, for: .normal)
    num8.setTitleColor(UIColor.black, for: .normal)
    num9.setTitleColor(UIColor.black, for: .normal)
    num0.setTitleColor(UIColor.black, for: .normal)
    spaceBar.setTitleColor(UIColor.black, for: .normal)
    plus.setTitleColor(UIColor.black, for: .normal)
    minus.setTitleColor(UIColor.black, for: .normal)
    back.setTitleColor(UIColor.black, for: .normal)
    left.setTitleColor(UIColor.black, for: .normal)
    right.setTitleColor(UIColor.black, for: .normal)
    period.setTitleColor(UIColor.black, for: .normal)
    comma.setTitleColor(UIColor.black, for: .normal)
    nextKeyboardButton.setTitleColor(UIColor.black, for: .normal)
    returnKey.setTitleColor(UIColor.black, for: .normal)

    num1.backgroundColor = UIColor.white
    num2.backgroundColor = UIColor.white
    num3.backgroundColor = UIColor.white
    num4.backgroundColor = UIColor.white
    num5.backgroundColor = UIColor.white
    num6.backgroundColor = UIColor.white
    num7.backgroundColor = UIColor.white
    num8.backgroundColor = UIColor.white
    num9.backgroundColor = UIColor.white
    num0.backgroundColor = UIColor.white
    spaceBar.backgroundColor = UIColor.white
    plus.backgroundColor = UIColor.white
    minus.backgroundColor = UIColor.white
    back.backgroundColor = UIColor.white
    left.backgroundColor = UIColor.white
    right.backgroundColor = UIColor.white
    nextKeyboardButton.backgroundColor = UIColor.white
    period.backgroundColor = UIColor.white
    comma.backgroundColor = UIColor.white
    returnKey.backgroundColor = UIColor.white

    connection.text = "No Connection"
    serialNum.text = "-"

    connection.backgroundColor = UIColor.white
    serialNum.backgroundColor = UIColor.white

    connection.layer.cornerRadius = 4
    serialNum.layer.cornerRadius = 4

    self.view.addSubview(connection)
    self.view.addSubview(serialNum)
    self.view.addSubview(spaceBar)
    self.view.addSubview(num1)
    self.view.addSubview(num2)
    self.view.addSubview(num3)
    self.view.addSubview(num4)
    self.view.addSubview(num5)
    self.view.addSubview(num6)
    self.view.addSubview(num7)
    self.view.addSubview(num8)
    self.view.addSubview(num9)
    self.view.addSubview(num0)
    self.view.addSubview(plus)
    self.view.addSubview(minus)
    self.view.addSubview(back)
    self.view.addSubview(left)
    self.view.addSubview(right)
    self.view.addSubview(period)
    self.view.addSubview(comma)
    self.view.addSubview(nextKeyboardButton)
    self.view.addSubview(returnKey)

}
@objc func returnButton(sender: UIButton){
    textDocumentProxy.insertText("\n")
}
@objc func addPeriod(sender: UIButton){
    textDocumentProxy.insertText(".")
}
@objc func addComma(sender: UIButton){
    textDocumentProxy.insertText(",")
}
@objc func leftOne(sender: UIButton){
    textDocumentProxy.adjustTextPosition(byCharacterOffset: -1)
}
@objc func rightOne(sender: UIButton){
    textDocumentProxy.adjustTextPosition(byCharacterOffset: 1)
}
@objc func deleteOne(sender: UIButton){
    textDocumentProxy.deleteBackward()
}
@objc func plusSign(sender: UIButton){
    textDocumentProxy.insertText("+")
}
@objc func minusSign(sender: UIButton){
    textDocumentProxy.insertText("-")
}
@objc func space(sender: UIButton){
    textDocumentProxy.insertText(" ")
}
@objc func addOne(sender: UIButton){
    textDocumentProxy.insertText("1")
    textDocumentProxy.insertText(String(ScreenHeight))

}
@objc func addTwo(sender: UIButton){
    textDocumentProxy.insertText("2")
}
@objc func addThree(sender: UIButton){
    textDocumentProxy.insertText("3")
}
@objc func addFour(sender: UIButton){
    textDocumentProxy.insertText("4")
}
@objc func addFive(sender: UIButton){
    textDocumentProxy.insertText("5")
}
@objc func addSix(sender: UIButton){
    textDocumentProxy.insertText("6")
}
@objc func addSeven(sender: UIButton){
    textDocumentProxy.insertText("7")
}
@objc func addEight(sender: UIButton){
    textDocumentProxy.insertText("8")
}
@objc func addNine(sender: UIButton){
    textDocumentProxy.insertText("9")
}
@objc func addZero(sender: UIButton){
    textDocumentProxy.insertText("0")
}


override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    if self.centralManager.state == CBManagerState.poweredOn {
        self.centralManager.scanForPeripherals(withServices: [lairdUUID], options: nil)
    }
}
@objc func keyboardWillShow(_ notification: NSNotification) {
    if let userInfo = notification.userInfo as? Dictionary<String, AnyObject>{
        let frame = userInfo[UIResponder.keyboardFrameEndUserInfoKey]
        let keyBoardRect = frame?.cgRectValue
        ScreenHeight = Int(keyBoardRect!.height)
    }
}
override func viewWillDisappear(_ animated: Bool) {
    //        super.viewWillDisappear(animated)
    //        if let centralManager = self.centralManager {
    //            if centralManager.state == CBManagerState.poweredOn {
    //                if let NUB5 = self.NUB5 {
    //                    if peripheral.state == CBPeripheralState.connected {
    //                        centralManager.cancelPeripheralConnection(NUB5)
    //                    }
    //                }
    //            }
    //        }
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated
}

override func textWillChange(_ textInput: UITextInput?) {
    // The app is about to change the document's contents. Perform any preparation here.
}

override func textDidChange(_ textInput: UITextInput?) {
    // The app has just changed the document's contents, the document context has been updated.
}

2 个答案:

答案 0 :(得分:0)

为防止崩溃和最佳实践,请使用

 NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)

viewWillAppear上。

答案 1 :(得分:0)

由于您的方法keyboardWillShow具有一个参数,因此您应该相应地更改语法,以将其必须查找的方法准确告知编译器。

NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(_:)), name: UIResponder.keyboardWillShowNotification, object: nil)

发生的是,编译器将搜索不带有任何不存在的参数的keyboardWillShow方法,因此不会执行任何操作。