【问题标题】:IOS keyboard for textview at bottom and searchbar at top用于底部文本视图和顶部搜索栏的 IOS 键盘
【发布时间】:2018-08-04 08:07:32
【问题描述】:

我在屏幕底部有一个文本视图,在屏幕顶部有一个搜索栏。以下是我在按下 textview 时解决键盘问题的代码

extension UIView { 
    func bindToKeyboard(){
        NotificationCenter.default.addObserver(self, selector: #selector(UIView.keyboardWillChange(_:)), name: NSNotification.Name.UIKeyboardWillChangeFrame, object: nil)
    }

    @objc func keyboardWillChange(_ notification: NSNotification) {
        let duration = notification.userInfo![UIKeyboardAnimationDurationUserInfoKey] as! Double
        let curve = notification.userInfo![UIKeyboardAnimationCurveUserInfoKey] as! UInt
        let curFrame = (notification.userInfo![UIKeyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue
        let targetFrame = (notification.userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
        let deltaY = targetFrame.origin.y - curFrame.origin.y

        UIView.animateKeyframes(withDuration: duration, delay: 0.0, options: UIViewKeyframeAnimationOptions(rawValue: curve), animations: {
            self.frame.origin.y += deltaY

        },completion: {(true) in
            self.layoutIfNeeded()
        })
    }
}

但是当我按下搜索栏时,屏幕向上移动,搜索栏消失。如果我这样做view.bindToKeyboard(),那么在显示键盘后编辑文本是正确的。

我尝试过的一个解决方案是将 textview 的出口绑定到键盘,但是一旦我开始输入,textview 就会消失。

【问题讨论】:

  • 你在用什么 UISearchBar 或 UISearchBarController?将您的配置放入搜索栏。
  • 你应该试试这个:stackoverflow.com/a/45826168/2553730
  • @guru 我正在使用 UISearchBar
  • 能否分享截图或小视频,让我们看看实际行为。
  • @guru 即将上传

标签: ios keyboard


【解决方案1】:

我认为问题在于您想知道键盘何时出现。搜索栏有一个文本字段。因此,当它被点击时,它会像您的 textview 一样打开键盘,并调用 keyboardWillChange

   keyboardWillChange(_ notification: NSNotification)

因此键盘出现并隐藏了您的搜索栏。您可以检测到搜索栏的点击并在那里取消通知。

    func searchBarShouldBeginEditing(_ searchBar: UISearchBar) -> Bool
    {
       //Dismiss your keyboard notification here

       return true
    }

您应该使用库来处理这种情况。我建议IQKeyboardManagerIQKeyBoardManager Github.

【讨论】:

    【解决方案2】:

    我编辑了我之前的回复...您是否尝试将您的搜索栏放入导航栏?

    类似这样的东西(只要放上 viewDidLoad):

    navigationItem.titleView = searchBar
    

    【讨论】:

    • 我收到一条错误消息,提示无法分配给属性:'inputView' 是一个只能获取的属性
    • 对不起..我的错误。你试过把你的searchBar放到navigationBar吗?
    • 按下底部的编辑栏时导航栏也会消失
    猜你喜欢
    • 1970-01-01
    • 2019-05-04
    • 2017-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-28
    • 1970-01-01
    相关资源
    最近更新 更多