我能够为UISearchController创建范围栏并设置它们的标题
resultSearchController = ({
let controller = UISearchController(searchResultsController: nil)
controller.searchResultsUpdater = self
controller.dimsBackgroundDuringPresentation = false
controller.searchBar.showsScopeBar = true
controller.searchBar.scopeButtonTitles = ["One", "two", "three"]
controller.searchBar.sizeToFit()
self.tableView.tableHeaderView = controller.searchBar
return controller
})()
Run Code Online (Sandbox Code Playgroud)
但是我如何使用范围栏进行实际排序.我目前的排序方法如下
func updateSearchResultsForSearchController(searchController: UISearchController) {
filteredTableData.removeAll(keepCapacity: false)
let searchPredicate = NSPredicate(format: "SELF CONTAINS[c] %@", searchController.searchBar.text)
let array = (tableData as NSArray).filteredArrayUsingPredicate(searchPredicate)
filteredTableData = array as! [String]
tableView.reloadData()
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试为iOS 8中的Contacts应用程序创建类似的体验.其主要组成部分是:
然而,这说起来容易做起来难.经过一段时间的挣扎tableHeaderView(不允许在桌面视图前进行交互,并且viewDidLayoutSubviews定位很复杂),我决定嵌入一个UITableView内部UIViewController,所以我可以添加UISearchBar为子视图.这非常有效,并允许在所有滚动位置与搜索栏进行交互,并且插图不难计算.
但是,激活后,搜索栏会在状态栏下方被切断.看起来像一个简单的问题 - 即使我没有在一个完全相同的实现中体验它UITableViewController.因此,我尝试确保在每个可能的视图控制器中设置所有属性以进行对齐.
self.edgesForExtendedLayout = UIRectEdgeNone;
self.extendedLayoutIncludesOpaqueBars = YES;
self.automaticallyAdjustsScrollViewInsets = NO;
Run Code Online (Sandbox Code Playgroud)
我在viewWillAppear和viewDidLoad以及半透明导航栏和表视图和搜索栏的不同起始帧中尝试过各种可能的组合.没运气.所以,我尝试调整帧或约束,可能使用topLayoutGuide或只是0.不幸的是,在任何UISearchControllerDelegate方法中调整帧实际上并没有调整其呈现位置,并且当活动动画开始时添加约束立即崩溃(由于super.top当时不存在于视图层次结构中;删除willPresent中的约束绝对没有任何结果.
经过一段时间的挣扎后,我尝试实施positionForBar作为UISearchBar代表:
- (UIBarPosition)positionForBar:(id <UIBarPositioning>)bar {
return UIBarPositionTopAttached;
}
Run Code Online (Sandbox Code Playgroud)
这似乎调整了高度,但搜索栏飞离了视图的顶部.激活后,搜索栏似乎直接出现在可见区域上方.这比在它后面或下面被剪裁更糟糕UIStatusBar.我还尝试在搜索控制器变为活动状态时隐藏状态栏,但有条件实现prefersStatusBarHidden根本不起作用(返回YES工作很好而没有UISearchController激活,但是当它处于活动时它再次显示状态栏或它显示在它下面).我认为这是因为UISearchController拒绝服从任何标准或规则,现在很痛苦.



我一直试图解决这个问题几天,除了UISearchController完全重新实现类/动画之外,我想不出解决方案.请帮忙!
objective-c uitableview uiviewcontroller ios uisearchcontroller
如何更改范围按钮(在点击范围后)更新搜索结果?当我再次输入时,搜索结果已更改(使用新范围)!
searchControl - config import UIKit
class ProductTableView: UIViewController, UITableViewDataSource, UITableViewDelegate, UISearchResultsUpdating
{
@IBOutlet weak var tableView: UITableView!
var searchController: UISearchController!
var friendsArray = [FriendItem]()
var filteredFriends = [FriendItem]()
override func viewDidLoad()
{
super.viewDidLoad()
searchController = UISearchController(searchResultsController: nil)
searchController.searchBar.sizeToFit()
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
searchController.searchBar.scopeButtonTitles = ["Title","SubTitle"]
definesPresentationContext = true
tableView.tableHeaderView = searchController.searchBar
self.tableView.reloadData()
}
Run Code Online (Sandbox Code Playgroud)
更新功能当我输入文本时,NSLog会打印我的文本和范围编号.当我改变范围 - 什么都没有!
func updateSearchResultsForSearchController(searchController: UISearchController) {
let searchText = searchController.searchBar.text
let scope = searchController.searchBar.selectedScopeButtonIndex
NSLog("searchText - \(searchText)")
NSLog("scope - \(scope)")
filterContents(searchText, scope: scope) …Run Code Online (Sandbox Code Playgroud) 我已经在SO上阅读了类似的问题和解决方案.但似乎没有一个能解决我的问题.我正在使用自定义搜索控制器和自定义搜索栏,并且没有调用func updateSearchResults(对于searchController:UISearchController).
var customSearchController: CustomSearchViewController!
Run Code Online (Sandbox Code Playgroud)
CustomSearchViewController:在ViewDidLoad()中
customSearchController = CustomSearchViewController(searchResultsController: ***nil***, searchBarFrame: CGRect(x: 0.0, y: 0.0, width: searchTableView.frame.size.width, height: 44.0), searchBarFont: UIFont(name: "HelveticaNeue", size: 16.0)!, searchBarTextColor: UIColor.purple, searchBarTintColor: UIColor.white)
customSearchController.searchResultsUpdater = self
customSearchController.definesPresentationContext = true
customSearchController.customSearchBar.placeholder = "What are you looking for?"
customSearchController.customSearchBar.backgroundColor = UIColor.white
customSearchController.customSearchBar.sizeToFit()
customSearchController.customSearchBar.resignFirstResponder()
customSearchController.customSearchBar.showsCancelButton = true
customSearchController.customSearchBar.delegate = self
Run Code Online (Sandbox Code Playgroud)
没有被召唤::(
func updateSearchResults(for searchController: UISearchController) {
filtered.removeAll()
filtered = searchArray.filter({ (text) -> Bool in
let tmp: NSString = text as NSString
let range = tmp.range(of: customSearchController.customSearchBar.text!, …Run Code Online (Sandbox Code Playgroud) 我们如何更改搜索控制器中取消按钮的标题?

它怎么UISearchController做呢?
UISearchBar为类似导航栏的视图?UISearchBar放入另一个视图层次结构并在以后恢复?UIViewController表示API 完成所有这些的?我可以在UISearchController不使用私有API 的情况下自行创建吗?
我在界面构建器中添加了一个搜索栏和搜索显示控制器到我的应用程序.我无法正确地进行deinit(dealloc).
它显示以下行为(swift2,ios9):
如果我在导航控制器中选择"返回"而不是选择项目,则会出现相同的行为.
code removed - refer to COMPLETE CODE at bottom of post.
Run Code Online (Sandbox Code Playgroud)
任何帮助赞赏!
更新进一步测试表明,从视图控制器中删除progressHud/loadingHud完全没有影响DEINIT没有被调用.它必须与tableview或searchcontroller本身有关...
更新2我尝试在viewWillDissapear中调用searchBarCancelButtonClicked()方法,但仍然没有释放.即使你点击"取消"然后导航它就会......
更新3将willDisappear/didDisappear更改为以下对DEINIT没有影响 - 但不会给出错误的界面问题(感谢Polina).我试图找出我能发布的任何东西,但到目前为止还没有运气.
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(true)
searchBarCancelButtonClicked(searchController.searchBar)
}
override func viewDidDisappear(animated: Bool) {
print("View did disappear")
searchController.searchBar.resignFirstResponder()
searchController.searchBar.endEditing(true)
searchController.active = false
loadingHud.removeFromSuperview()
progressHud.removeFromSuperview()
searchController.searchBar.delegate = nil
searchController.searchResultsUpdater = nil
searchController = nil
tableView = nil
super.viewDidDisappear(true)
}
Run Code Online (Sandbox Code Playgroud)
更新4我找不到答案.真的希望有人可以帮忙!
更新5响应@ConfusedByCode - 我更新了以下方法,以便[unowned self] in在所有闭包或后台线程操作中使用:
code removed - …Run Code Online (Sandbox Code Playgroud) 我的tableview标题中有一个搜索栏.为此,我正在使用uisearchcontroler.但是当我在搜索栏中发短信时它会更新tablview数据,我需要在点击键盘上的搜索按钮时更新tablview,因为我在api中获取搜索数据,每次当我在搜索栏中发短信时它都会请求并且需要很长时间.我怎么解决这个问题?
var resultSearchController = UISearchController()
var indicator:UIActivityIndicatorView = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.Gray)
var ref=UIRefreshControl()
override func viewDidLoad() {
super.viewDidLoad()
self.resultSearchController = ({
let controller = UISearchController(searchResultsController: nil)
controller.searchResultsUpdater = self
controller.dimsBackgroundDuringPresentation = false
controller.searchBar.sizeToFit()
self.tableView.tableHeaderView = controller.searchBar
return controller
})()
indicator.frame = CGRectMake(0.0, 0.0, 40.0, 40.0);
indicator.center = view.center
view.addSubview(indicator)
indicator.bringSubviewToFront(view)
indicator.startAnimating()
UIApplication.sharedApplication().networkActivityIndicatorVisible = true
getRequests.getType1(tableView,spinner: indicator)
ref.addTarget(self, action: #selector(Catalog1TableViewController.refresh(_:)), forControlEvents: UIControlEvents.ValueChanged)
ref.attributedTitle = NSAttributedString(string: "????????")
tableView.addSubview(ref)
}
func updateSearchResultsForSearchController(searchController: UISearchController)
{
indicator.startAnimating()
UIApplication.sharedApplication().networkActivityIndicatorVisible = true
tableView.reloadData()
getRequests.getProduct(tableView, spinner: indicator,name: searchController.searchBar.text!)
for …Run Code Online (Sandbox Code Playgroud) 有没有人成功地保留了UISearchBar在tableHeaderView一个UITableView在ios11?iPhone X在景观中通常会出现问题:
Apple建议使用新searchController属性:
if (@available(iOS 11.0, *)) {
self.navigationItem.searchController = self.searchController;
self.navigationItem.hidesSearchBarWhenScrolling = NO;
} else {
self.tableView.tableHeaderView = self.searchController.searchBar;
}
Run Code Online (Sandbox Code Playgroud)
然而,这并不总是有效.这是一个代码示例:
嵌入UISplitViewController时,UINavigationBar中缺少iOS11 UISearchBar
所以,试图保持searchBar在tableHeaderView-切实可行的第一步是应用适当的自动布局限制:
if (@available(iOS 11.0, *)) {
UILayoutGuide *guide = self.tableView.safeAreaLayoutGuide;
searchBar.translatesAutoresizingMaskIntoConstraints = NO;
[searchBar.leftAnchor constraintEqualToAnchor:guide.leftAnchor].active = YES;
[searchBar.rightAnchor constraintEqualToAnchor:guide.rightAnchor].active = YES;
}
Run Code Online (Sandbox Code Playgroud)
在searchBar显示时,那么正确的尺寸:
但是,UISearchBar激活时会出现问题:
这似乎是一个老问题.有许多stackoverflow问题和答案与这样的问题有关.在这种情况下,在检查视图层次结构时,似乎设置的宽度UISearchBar不正确.所以这可以纠正:
- (void)didPresentSearchController:(UISearchController *)searchController NS_AVAILABLE_IOS(11_0)
{
if (@available(iOS 11.0, *)) {
CGRect frame = …Run Code Online (Sandbox Code Playgroud) 我想实现UISearchController,使用本教程一步一步:
使用MKLocalSearchRequest搜索地点并使用UISearchController显示结果
在本节中......
...我在步骤3中遇到问题.设置表视图数据源
extension LocationSearchTable {
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return matchingItems.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell")!
let selectedItem = matchingItems[indexPath.row].placemark
cell.textLabel?.text = selectedItem.name
cell.detailTextLabel?.text = ""
return cell
}
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试override从cellForRowAtindexPathtableView函数中删除,在这种情况下我在Xcode中没有错误,但执行后出错:
Terminating app due to uncaught exception 'NSInternalInconsistencyException',
reason: 'UITableView (<UITableView: 0x7ff8e03a3800; frame = (0 0; 375 667); clipsToBounds = YES; autoresize = W+H;
gestureRecognizers = <NSArray: …Run Code Online (Sandbox Code Playgroud) ios ×8
swift ×5
uitableview ×5
cocoa-touch ×1
ios11 ×1
ios8 ×1
mapkit ×1
objective-c ×1
retain ×1
scope ×1
search ×1
searchbar ×1
uikit ×1
uisearchbar ×1