我正在为 iOS 应用程序使用 swift 2.2。
我有一个带有 .TouchUpInside
事件的 UIButton,该事件触发 UITextField
成为第一响应者
。
下面是输入:
self.manualInput = BrandTextField(y: 0, parentWidth: self.view.frame.width)
self.manualInput.returnKeyType = .Search
self.manualInput.autocorrectionType = .No
self.manualInput.hidden = true
self.manualInput.text = ""
self.manualInput.delegate = self
self.manualInput.autocapitalizationType = .AllCharacters
self.manualInput.moveX(0)
self.manualInput.changeWidth(self.view.frame.width)
self.manualInput.layer.cornerRadius = 0
self.backgroundView.addSubview(self.manualInput)
下面是按钮:
self.keyboardButton = ActionButton(x: Dimensions.xScaleValue(170), y: Dimensions.yScaleValue(495), onTitle: "Enter code", offTitle: "Enter code", onImage: "manual_entry", offImage: "manual_entry", imageOn: true, action: {
self.manualInput.becomeFirstResponder()
print("Open the input")
})
self.backgroundView.addSubview(self.keyboardButton)
我还有这两个观察者:
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.keyBoardWillShow), name: UIKeyboardDidShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.keyBoardWillHide), name: UIKeyboardWillHideNotification, object: nil)
func keyBoardWillShow(notification: NSNotification) {
print("Keyboard will show")
let frame = (notification.userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()
self.manualInput.moveY(frame.origin.y - self.manualInput.frame.height)
self.manualInput.hidden = false
}
func keyBoardWillHide(notification: NSNotification) {
print("Keyboard will hide")
self.manualInput.moveY(0)
self.manualInput.hidden = true
}
问题是,从 iOS 11 开始,当我按下按钮时,会点击 becomeFirstResponder() 行,但不会显示键盘。
这里对我来说很奇怪的是,如果我在 View 中初始化输入并使其不隐藏,按钮仍然不起作用。除非我点击输入,关闭键盘然后使用按钮触发。
我不知道我是否在为 iOS 11 设置此设置时遗漏了什么,或者我是否不能再这样做了?
最佳答案
是否确保在主线程中调用becomeFirstResponder
?
DispatchQueue.main.async {
self.manualInput.becomeFirstResponder()
}
我遇到过类似的情况,按上述方法操作后,它可以正常工作。
关于iOS 11 - 成为第一响应者不打开键盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46849828/