【问题标题】:Get sender on NotificationCenter keyboard will show在 NotificationCenter 键盘上获取发件人将显示
【发布时间】:2017-01-19 03:45:44
【问题描述】:

我目前正在开发一个应用程序,该应用程序在一个滚动视图中包含多个文本字段和 2 个文本视图。在键盘出现时,我已经能够更改滚动视图的内容插图,以允许字段不被隐藏。我遇到的问题是我只能让它在一个硬编码字段上工作。我必须选择一个并为其设置动画。无论如何可以在keyboardWillShow 上获取发件人字段或文本视图?目前,我正在使用它并且一切正常,除了提到我必须选择一个字段detailsTxtView 并为其设置动画。有什么帮助吗?

NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillShow(notification:)),name: NSNotification.Name.UIKeyboardWillShow, object: nil)

func keyboardWillShow(notification: NSNotification) {
    if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
        let contentInsets: UIEdgeInsets? = UIEdgeInsetsMake(0.0, 0.0, keyboardSize.height, 0.0)
        scrollView.contentInset = contentInsets!
        scrollView.scrollIndicatorInsets = contentInsets!
        let goto = CGPoint(x: CGFloat(0.0), y: CGFloat(detailsTxtView.frame.origin.y + (keyboardSize.height + 40)))
        scrollView.setContentOffset(goto, animated: true)
    }

}

【问题讨论】:

    标签: ios swift swift3 nsnotificationcenter


    【解决方案1】:

    在 iOS 中,您可以通过 UIResponder.isFirstResponder 确定谁拥有键盘焦点(注意所有 UIView 都继承自 UIResponder,因此它是每个视图的一个属性)。只需检查哪个字段具有 isFirstResponder = true 并滚动到该字段。如果您有很多字段,则将所有字段放入一个出口集合中很有用,这样您就可以遍历它们并找出哪个是FirstResponder。

    【讨论】:

    • 谢谢。我将在未来的许多项目中利用这些知识
    猜你喜欢
    • 1970-01-01
    • 2015-12-15
    • 2013-04-17
    • 2011-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-04
    相关资源
    最近更新 更多