简体   繁体   English

使用 UISearchBar 关闭键盘,而不退出第一响应者

[英]Dismissing the keyboard with UISearchBar, without resigning first responder

Hey, I was working on a navigation-based app on iPhone similar to the contacts app.嘿,我正在 iPhone 上开发一个基于导航的应用程序,类似于联系人应用程序。 When you input something in the search bar, and scroll in the table (in the contacts app), the keyboard goes away.当您在搜索栏中输入内容并在表格中滚动(在联系人应用程序中)时,键盘会消失。 I don't think it resigns first responder though, because when I try and do that in -(void)scrollViewDidScroll:(UIScrollView *)scrollView, it disables the cancel button, which does not happen in the contacts app.不过,我认为它不会让第一响应者辞职,因为当我尝试在 -(void)scrollViewDidScroll:(UIScrollView *)scrollView 中执行此操作时,它会禁用取消按钮,这在联系人应用程序中不会发生。 Basically my question is how do I dismiss the keyboard without disabling the cancel button, like in the contacts app?基本上我的问题是如何在不禁用取消按钮的情况下关闭键盘,就像在联系人应用程序中一样?

Thanks谢谢

Well, just ran into this very old question.好吧,刚刚遇到了这个非常古老的问题。 You can enable the 'cancel' button when you start to scroll as follows:您可以在开始滚动时启用“取消”按钮,如下所示:

func scrollViewWillBeginDragging(scrollView: UIScrollView) {
    searchBar.resignFirstResponder()
    let searchCancelButton = searchBar.valueForKey("cancelButton") as! UIButton
    searchCancelButton.enabled = true // <-- THIS line is the trick

}

Swift 4斯威夫特 4

func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
    searchBar.resignFirstResponder()
    let cancelButton = searchBar.value(forKey: "cancelButton") as! UIButton
    cancelButton.isEnabled = true
}

tableView.keyboardDismissMode = .onDrag添加到viewDidLoad()就像一个魅力。

I tried multiple options but all the desired output is not achieved, like hiding the cancel along with keyboard.我尝试了多个选项,但未实现所有所需的输出,例如将取消与键盘一起隐藏。

So I used searchBar.inputAccessoryView which will be visible above the keyboard with a button at right (toolbar).所以我使用了searchBar.inputAccessoryView ,它将在键盘上方可见,右侧有一个按钮(工具栏)。

    class AnyViewClass{

        func createSearch(){
            searchBar.inputAccessoryView = ViewsFactory.createAccessoryView(withButtonType:.done,target: self, action:#selector(YourClass.keyboardDonePressed))
        }


        @objc private func keyboardDonePressed(){
             searchBar?.resignFirstResponder()
             searchBar?.text = nil
        }

}

class ViewsFactory {

    class func createAccessoryView(withButtonType type:UIBarButtonItem.SystemItem,
                                   target: Any?,
                                   action: Selector?) -> UIToolbar{

        var accessoryDoneButton: UIBarButtonItem!
        let accessoryToolBar = UIToolbar(frame: CGRect(x: 0,
                                                       y: 0,
                                                       width: UIScreen.main.bounds.width,
                                                       height: 44))

        let emptySpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace,
                                         target: nil,
                                         action: nil)
        accessoryDoneButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.done,
                                              target: target,
                                              action: action)
        accessoryDoneButton.tintColor = <any color>
        accessoryToolBar.items = [emptySpace, accessoryDoneButton]
        return accessoryToolBar

    }
}

(As hinted at in the self-answering comment) If you want to get table search behavior like the built-in contacts app, you can't just slap a UISearchBar into a view with a UITableView, instead you need to use a UITableViewController together with a Search Display Controller . (正如在自我回答评论中所暗示的)如果你想获得像内置联系人应用程序一样的表格搜索行为,你不能只是将 UISearchBar 拍打到带有 UITableView 的视图中,而是需要一起使用 UITableViewController带有Search Display Controller This sample app is a perfect guide:这个示例应用程序是一个完美的指南:

https://developer.apple.com/library/ios/#samplecode/TableSearch/Introduction/Intro.html https://developer.apple.com/library/ios/#samplecode/TableSearch/Introduction/Intro.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM