如果用户双击文本字段,键盘会变黑
Posted
技术标签:
【中文标题】如果用户双击文本字段,键盘会变黑【英文标题】:Keyboard goes black if user double taps textfield 【发布时间】:2015-11-12 20:22:13 【问题描述】:我有一个注册页面,其中有 3 个文本字段。触摸文本字段时视图会向上滑动,以便在显示键盘时可以看到字段。但是,如果用户快速点击文本字段两次,而不是点击一次,则键盘会变黑,并且视图定位会变得不可挽回。这是问题的照片,代码如下,在此先感谢]1
override func viewDidLoad()
super.viewDidLoad()
emailTextField.delegate = self
passwordTextField.delegate = self
verifyPasswordTextField.delegate = self
emailTextField.autocorrectionType = UITextAutocorrectionType.No
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name: UIKeyboardWillHideNotification, object: nil)
// method to move the view up when keyboard appears
func keyboardWillShow(notification: NSNotification)
if viewIsLifted == false
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue()
// hide the logo and name when view slides up
UIView.animateWithDuration(0.2, delay: 0.0, options: UIViewAnimationOptions.CurveEaseInOut, animations: () -> Void in
self.likemojiIcon.alpha = 0
self.likemojiLabel.alpha = 0
, completion: nil)
self.view.frame.origin.y -= keyboardSize.height
viewIsLifted = true
// method to slide view back down when keyboard is resigned
func keyboardWillHide(notification: NSNotification)
if viewIsLifted == true
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue()
// make logo and name reappear on slide back down
UIView.animateWithDuration(0.2, delay: 0.0, options: UIViewAnimationOptions.CurveEaseInOut, animations: () -> Void in
self.likemojiIcon.alpha = 1
self.likemojiLabel.alpha = 1
, completion: nil)
self.view.frame.origin.y += keyboardSize.height
viewIsLifted = false
【问题讨论】:
【参考方案1】:当您收到键盘将出现的通知时关闭用户交互 (beginIgnoringInteractionEvents
),当您收到键盘通知时将其重新打开 (endIgnoringInteractionEvents
) >确实出现了。
【讨论】:
【参考方案2】:在动画开始前设置 viewIsLifted = true ,否则双击可能会在变量更改之前注册两次键盘活动,从而导致崩溃。在旁注中,通知在哪里发布?
【讨论】:
非常感谢您的回复。我在动画之前将顺序更改为 viewIsLifted = true ,但它仍然在快速双击时使用相同的黑色键盘。还有其他想法吗?以上是关于如果用户双击文本字段,键盘会变黑的主要内容,如果未能解决你的问题,请参考以下文章