【问题标题】:Properly dismiss keyboard in swift for collectionView为collectionView正确关闭键盘
【发布时间】:2015-08-22 04:04:40
【问题描述】:

我正在创建一个简单的信使聊天窗口,并且我正在使用UICollectionView 来发送我的气泡消息。 现在我想设置键盘以正确显示和隐藏。 我使用了NSNotifications 并为keyboardWillShow:keyboardWillHide: 事件创建了函数。我还为我的CollectionView 设置了keyboardDismissMode 到 Interactive。 所以现在,当我向上滚动时,我的键盘被隐藏(由交互式关闭模式引起),我也得到了keyboardWillHide 事件,它重置了我的 UIEdgeInsets。所以基本上在我向上滚动并且我的键盘被隐藏后,我的scrollView 立即进入底部。我的目标是让它像在 iMessage.app、WhatsApp 等中一样工作。我将不胜感激任何帮助或建议!

这是我的代码:

class ViewController: UIViewController {

   @IBOutlet weak var collectionView: UICollectionView!
   var customView: CustomView!

    override func viewDidLoad() {
       super.viewDidLoad()
       customView = CustomView(frame: CGRectMake(0, 0, UIScreen.mainScreen().bounds.width, 40.0))
       customView.textView!.delegate = self
       collectionView.scrollToBottom(true)
       collectionView.keyboardDismissMode = UIScrollViewKeyboardDismissMode.Interactive

       NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShow:", name: UIKeyboardWillShowNotification, object: nil)
       NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillHide:", name: UIKeyboardWillHideNotification, object: nil)

    }

   override var inputAccessoryView: UIView! {
       get {
           if customView == nil {
               customView = CustomView()
           }
           return customView
          }
       }

   override func canBecomeFirstResponder() -> Bool {
      return true
   }

   func keyboardWillShow(notification: NSNotification) {
        let userInfo = notification.userInfo ?? [:]
        let keyboardFrame = (userInfo[UIKeyboardFrameBeginUserInfoKey] as! NSValue).CGRectValue()
        let adjustmentHeight = (CGRectGetHeight(keyboardFrame) + CGRectGetHeight(customView.frame) )
        let contentInset:UIEdgeInsets
        if UIInterfaceOrientationIsPortrait(UIApplication.sharedApplication().statusBarOrientation) {
            contentInset = UIEdgeInsetsMake(0, 0, adjustmentHeight, 0)
        } else {
            contentInset = UIEdgeInsetsMake(0, 0, keyboardFrame.width, 0)
        }
        collectionView.contentInset = contentInset
        collectionView.scrollIndicatorInsets = contentInset
        collectionView.scrollToBottom(true)
  }

   func keyboardWillHide(notification: NSNotification) {
       collectionView.contentInset = UIEdgeInsetsZero
       collectionView.scrollIndicatorInsets = UIEdgeInsetsZero
   }



}

【问题讨论】:

    标签: ios swift uiscrollview keyboard uicollectionview


    【解决方案1】:

    当您以交互方式关闭键盘时,您的 collectionView 滚动到底部,这不是因为 UIEdgeInsets 被重置为零,而是因为在此配置中,当您以交互方式关闭键盘时,keyboardWillShow 方法会再次被调用。大概是因为inputAccessoryView。要验证这一点,请在您的 keyboardWillShow 方法中放置一个 println,然后再试一次。我想说这是评论而不是答案,但我只有 47 个代表,需要 50 个评论。但我认为这些信息可能对您有用。

    【讨论】:

    • 是的,你完全正确。当我的应用程序启动并且我的键盘被隐藏时,keyboardWillShow 被调用,当我关闭键盘时也是如此。也许我应该使用另一种方法,例如 keyboardDidShow ?
    • 我怀疑它与 inputAccessoryView 在键盘隐藏和调用后重新引入有关。您可能需要重新考虑 inputAccessoryView 的呈现方式。此外,您的应用启动时的初始调用是因为正在添加 inputAccessoryView 并因此修改了键盘。
    • 好吧,我移动了代码,它在 textFieldDidBeginEditing 中插入,它工作得更好:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-28
    • 2023-01-24
    • 2017-03-08
    • 2013-01-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多