iOS 11 - 成为第一响应者不打开键盘

标签 ios swift ios11

我正在为 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/

相关文章:

ios - 自定义 tableview 单元格下的 UILabel 未更新

ios - 是否可以找到也有Apple设备的用户的联系人?

ios - Swift如何改变backIndicatorImage的tintColor

ios - 日历事件添加错误时间问题

ios - tableviewcell 的附件 View 中的 UISwitch,通过选择器传递参数以使选择器函数看到 indexpath.row?

ios - UITableViewDropCoordinator 下降(_ :toRowAt:) doesn't perform animation

swift - 更改 UINavigationbar 中元素的色调颜色(iOS 11)

ios - 向上或向下滚动 UITableview 时更改值

ios - Swift 如何将 String 从类转换为 @State String 变量?

ios - ARKit - 对象未放置