簡體   English   中英

iOS 11 - 成為第一響應者而不是打開鍵盤

[英]iOS 11 - Become First Responder not opening the keyboard

我正在使用swift 2.2 for iOS app。

我有一個帶有.TouchUpInside事件的UIButton,觸發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時的按鈕時,會觸發becomeFirstResponder()行,但不會顯示鍵盤。

對我來說,奇怪的是,如果我初始化視圖中的輸入並使其不隱藏按鈕仍然不起作用。 除非我點擊輸入,否則關閉鍵盤,然后使用按鈕觸發。

我不知道我是否在iOS 11的設置中遺漏了一些內容,或者我是否可以不再這樣做了?

你確保在主線程中調用becomeFirstResponder嗎?

DispatchQueue.main.async {
    self.manualInput.becomeFirstResponder()
}

我遇到了類似的情況,我按照上面的說法正常工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM