![](/img/trans.png)
[英]UIScrollView (containing number of UITextFields) is bouncing up and down while switching from predictive keyboard to simple keyboard (iOS 8)
[英]iOS - Keyboard Notifications not firing while switching UITextfields
通知發生了奇怪的現象。 每當我將一個UITextfield
切換到另一個UITextfield
時,我的通知都將正確觸發。 但是最近我注意到,當從一個UITextField
切換到另一個UITextField
,它們並沒有被觸發,但是當鍵盤被隱藏時,它們卻可以正確觸發。 蘋果改變了這種邏輯嗎?
我需要將字段滾動到可見狀態,並且由於切換時觀察者現在不觸發,因此字段不會變為可見矩形。
我已經通過在textFieldDidBeginEditing
上發布通知來創建替代方法,但是如果可能的話,我想返回舊方法。
我找到了問題的真正原因。 如果UITextfield
沒有附件視圖,則切換時不會調用鍵盤通知,在附件視圖的情況下,每次都將調用鍵盤通知( 為什么每次選擇另一個TextField時都會調用UIKeyboardWillShowNotification? )
就我而言,我在每個字段上都有附件視圖,但是在添加附件視圖時,我在所有字段中都添加了相同的視圖,這才是真正的問題。 我需要將UIView的單獨實例分配給不同的字段。 一經發現我的問題就解決了。
我遇到了完全相同的問題,並通過使用一個與我的實際按鈕重復的新按鈕創建了一個附件視圖來解決了這個問題。 因此,基本上,請考慮您有一個UIButton
粘在UIViewController
的底部,並且您有2個UITextField
,當互換切換時,它們不會感覺到UIButton
曾被移到其他地方但仍被卡在鍵盤上。 以下這段代碼說明了如何解決此問題:
@IBOutlet weak var signInBtn: UIButton!
@IBOutlet weak var passwordTextField: UITextField!
@IBOutlet weak var emailTextField: UITextField!
var accessoryViewKeyboard:UIView?
var btnAccessory:UIButton?
override func viewDidLoad() {
super.viewDidLoad()
passwordTextField.delegate = self
emailTextField.delegate = self
accessoryViewKeyboard = UIView(frame: signInBtn.frame)
//Inputting the "accessoryViewKeyboard" here as the "inputAccessoryView" is of
//utmost importance to help the "signInBtn" to show up on tap of different "UITextFields"
emailTextField.inputAccessoryView = accessoryViewKeyboard
passwordTextField.inputAccessoryView = accessoryViewKeyboard
setupBtnWithKeyboard()
}
func setupBtnWithKeyboard() {
btnAccessory = UIButton(frame: CGRect(x: signInBtn.frame.origin.x, y: signInBtn.frame.origin.y, width: self.view.frame.size.width, height: signInBtn.frame.size.height))
accessoryViewKeyboard?.addSubview(btnAccessory!)
btnAccessory?.translatesAutoresizingMaskIntoConstraints = false
btnAccessory?.frame = CGRect(x: (accessoryViewKeyboard?.frame.origin.x)!,
y: (accessoryViewKeyboard?.frame.origin.y)!,
width: self.view.frame.size.width,
height: (accessoryViewKeyboard?.frame.size.height)!)
btnAccessory?.backgroundColor = UIColor(red: 31/255, green: 33/255, blue: 108/255, alpha: 1)
btnAccessory?.setTitle("Sign In", for: .normal)
btnAccessory?.titleLabel?.font = .systemFont(ofSize: 22)
btnAccessory?.titleLabel?.textColor = UIColor.white
btnAccessory?.titleLabel?.textAlignment = .center
btnAccessory?.isEnabled = true
btnAccessory?.addTarget(self, action: #selector(SignIn.signInBtnPressed), for: .touchUpInside)
NSLayoutConstraint.activate([
btnAccessory!.leadingAnchor.constraint(equalTo:
accessoryViewKeyboard!.leadingAnchor, constant: 0),
btnAccessory!.centerYAnchor.constraint(equalTo:
accessoryViewKeyboard!.centerYAnchor),
btnAccessory!.trailingAnchor.constraint(equalTo:
accessoryViewKeyboard!.trailingAnchor, constant: 0),
btnAccessory!.heightAnchor.constraint(equalToConstant: signInBtn.frame.size.height),
])
}
這樣就完成了。 這將使UIButton
始終出現在鍵盤上。 重要的是,無論多少的情況UITextField
你介紹,總輸入accessoryViewKeyboard
為inputAccessoryView
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.