我正在使用 swift 2.2 作为 iOS 应用程序。
我有一个 UIButton,其中包含一个.TouchUpInside
事件,该事件触发UITextField
成为first responder
。
以下是输入:
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开始按下按钮时,成为FirstResponder()行被击中,但没有显示键盘。
对我来说奇怪的是,如果我初始化视图中的输入并使其不隐藏,按钮仍然不起作用。除非我单击输入,否则请关闭键盘,然后使用按钮触发。
我不知道我是否在iOS 11的设置中遗漏了一些东西,或者我是否无法再这样做?
您是否确保在主线程中调用becomeFirstResponder
?
DispatchQueue.main.async {
self.manualInput.becomeFirstResponder()
}
我遇到过类似的情况,在我按照上述操作后,它工作正常。
- 参考:bebe 成为 FirstResponder() 应该总是在主线程上调用吗?