gpt4 book ai didi

swift - searchBarController 搜索时不跟随文本,取消不起作用

转载 作者:行者123 更新时间:2023-11-30 13:44:11 25 4
gpt4 key购买 nike

我关注了this创建自定义搜索栏的教程,但是我遇到了几个问题。当我在搜索栏中输入文本并且我要越过文本框时,文本就会消失,它不会像应有的那样跟随指针。

我遇到的第二个问题是取消按钮无法正常工作,我必须遵循 CustomSearchController 类中的一段代码

    func searchBarCancelButtonClicked(searchBar: UISearchBar) {
customSearchBar.resignFirstResponder()
customDelegate.didTapOnCancelButton()
}

但是当点击取消按钮时,文本仍保留在搜索栏中

有谁知道我哪里出了问题或者我可以做些什么来解决这些问题。

编辑:好吧,第一个问题似乎出在搜索文本框的字体大小上。我将其设置为 20,但任何高于 18 的值都会停止跟随文本。我该如何解决这个问题。下面的代码显示了我如何设置customSearchController

customSearchController = CustomSearchController(searchResultsController: self, searchBarFrame: CGRectMake(0.0, 0.0, tblSearchResults.frame.size.width, 100.0), searchBarFont: UIFont(name: "Futura", size: 20)!, searchBarTextColor: UIColor.greenColor(), searchBarTintColor: UIColor.blackColor())

最佳答案

尝试按以下方式进行操作。您应该了解它是如何工作的。我假设您的 UI Controller 是表一:

public class YourController: UITableViewController, UISearchBarDelegate, UISearchResultsUpdating {


private var searchController: UISearchController? // your search controller


override public func viewDidLoad() {
super.viewDidLoad()


// create search controller

self.searchController = UISearchController(searchResultsController: nil)
self.searchController!.searchResultsUpdater = self
self.searchController!.dimsBackgroundDuringPresentation = false
self.searchController!.hidesNavigationBarDuringPresentation = false
self.searchController!.searchBar.delegate = self
self.searchController!.searchBar.returnKeyType = .Done
self.searchController!.searchBar.enablesReturnKeyAutomatically = true
self.searchController!.searchBar.sizeToFit()
self.searchController!.searchBar.searchBarStyle = .Minimal
self.tableView.tableHeaderView = self.searchController!.searchBar // add search bar to table header

}


public func updateSearchResultsForSearchController(searchController: UISearchController) {
// called after the user enters some text into the search bar
if let searchString = searchController.searchBar.text?.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet()) where !searchString.isEmpty {

// you should filter the table array here and load your table with those data
}
self.tableView.reloadData() // reload your table
}


public func searchBarSearchButtonClicked(searchBar: UISearchBar) {
// some action after user clicks on the return button if needed
}

public func searchBarCancelButtonClicked(searchBar: UISearchBar) {
// search canceled -> you should add all the objects back here to the Table Array
}


public func searchBar(searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) {
self.updateSearchResultsForSearchController(self.searchController!)
}

}

或者您可以通过查看以下内容获得一些想法: Some Github example

关于swift - searchBarController 搜索时不跟随文本,取消不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35181272/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com