显示带有操作的搜索栏(栏项)
Show search bar with action (bar item)
我的搜索栏有问题。我需要在右上角创建一个带有搜索按钮的 table 视图,当我点击它时,它应该会显示搜索栏。
我的代码在这里:
// Search controller
searchController = ({
let controller = UISearchController(searchResultsController: nil)
controller.delegate = self
controller.searchBar.delegate = self
controller.searchResultsUpdater = self
controller.dimsBackgroundDuringPresentation = false
controller.hidesNavigationBarDuringPresentation = true
controller.searchBar.sizeToFit()
return controller
})()
这是行动:
// Search action
@IBAction func search(sender: UIBarButtonItem) {
print("Open search")
searchController.active = true
if searchController.searchBar.isFirstResponder() == false {
searchController.searchBar.becomeFirstResponder()
}
}
当我点击按钮时,没有任何反应(只在控制台打印文本),我想要的是下图:
您的 class 需要符合 UISearchBarDelegate,我不确定您是否已经这样做了。还要确保您呈现搜索视图
来自 Apple 的文档:
The UISearchBarDelegate protocol defines the optional methods you implement to make a UISearchBar control functional. A UISearchBar object provides the user interface for a search field on a bar, but it’s the application’s responsibility to implement the actions when buttons are tapped. At a minimum, the delegate needs to perform the actual search when text is entered in the text field.
这是我的应用中的示例
@IBAction func searchAction(sender: UIBarButtonItem) {
// Create the search controller and specify that it should present its results in this same view
searchController = UISearchController(searchResultsController: nil)
// Set any properties (in this case, don't hide the nav bar and don't show the emoji keyboard option)
searchController.hidesNavigationBarDuringPresentation = false
searchController.searchBar.keyboardType = UIKeyboardType.ASCIICapable
// Make this class the delegate and present the search
self.searchController.searchBar.delegate = self
presentViewController(searchController, animated: true, completion: nil)
}
老问题,但想添加一个新答案(也许自 2016 年的原始答案以来情况发生了变化)。
假设您已经设置了搜索控制器,只需在视图控制器上调用它。无需符合 UISearchBarDelegate:
Swift 5
present(searchController, animated: true, completion: nil)
我的搜索栏有问题。我需要在右上角创建一个带有搜索按钮的 table 视图,当我点击它时,它应该会显示搜索栏。
我的代码在这里:
// Search controller
searchController = ({
let controller = UISearchController(searchResultsController: nil)
controller.delegate = self
controller.searchBar.delegate = self
controller.searchResultsUpdater = self
controller.dimsBackgroundDuringPresentation = false
controller.hidesNavigationBarDuringPresentation = true
controller.searchBar.sizeToFit()
return controller
})()
这是行动:
// Search action
@IBAction func search(sender: UIBarButtonItem) {
print("Open search")
searchController.active = true
if searchController.searchBar.isFirstResponder() == false {
searchController.searchBar.becomeFirstResponder()
}
}
当我点击按钮时,没有任何反应(只在控制台打印文本),我想要的是下图:
您的 class 需要符合 UISearchBarDelegate,我不确定您是否已经这样做了。还要确保您呈现搜索视图
来自 Apple 的文档:
The UISearchBarDelegate protocol defines the optional methods you implement to make a UISearchBar control functional. A UISearchBar object provides the user interface for a search field on a bar, but it’s the application’s responsibility to implement the actions when buttons are tapped. At a minimum, the delegate needs to perform the actual search when text is entered in the text field.
这是我的应用中的示例
@IBAction func searchAction(sender: UIBarButtonItem) {
// Create the search controller and specify that it should present its results in this same view
searchController = UISearchController(searchResultsController: nil)
// Set any properties (in this case, don't hide the nav bar and don't show the emoji keyboard option)
searchController.hidesNavigationBarDuringPresentation = false
searchController.searchBar.keyboardType = UIKeyboardType.ASCIICapable
// Make this class the delegate and present the search
self.searchController.searchBar.delegate = self
presentViewController(searchController, animated: true, completion: nil)
}
老问题,但想添加一个新答案(也许自 2016 年的原始答案以来情况发生了变化)。
假设您已经设置了搜索控制器,只需在视图控制器上调用它。无需符合 UISearchBarDelegate:
Swift 5
present(searchController, animated: true, completion: nil)