我正在尝试以UISearchDisplayController编程方式创建.我有一个应该设置我的搜索控制器的方法,但是当我调用它时,没有任何反应.
这是我的-setupSearch方法:
- (void)setupSearch {
UISearchBar *myBar;
UISearchDisplayController *myCon;
myBar = [[UISearchBar alloc] initWithFrame:CGRectZero];
[myBar sizeToFit];
myCon = [[UISearchDisplayController alloc]
initWithSearchBar:myBar contentsController:self];
[myBar release];
myCon.delegate = self;
myCon.searchResultsDataSource = self;
myCon.searchResultsDelegate = self;
/* Setup scopes */
{
NSMutableArray *scopes;
NSUInteger count, i;
NSString *aScope;
count = SCOPE_COUNT;
scopes = [[NSMutableArray alloc] initWithCapacity:count];
for(i = 0; i < count; i++) {
// I create four scopes here
}
myCon.searchBar.scopeButtonTitles = scopes;
[scopes release];
}
[myCon release]; …Run Code Online (Sandbox Code Playgroud) 我希望我的搜索栏将其背景向上扩展到状态栏下方,如下所示:

这是上图中的相应代码:
- (void)viewDidLoad
{
[super viewDidLoad];
self.searchBar = [[UISearchBar alloc] init];
self.searchBar.delegate = self;
[self.view addSubview:self.searchBar];
self.searchBar.translatesAutoresizingMaskIntoConstraints = NO;
NSDictionary *views = @{@"v":self.searchBar,
@"topLayoutGuide":self.topLayoutGuide};
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[topLayoutGuide][v]" options:0 metrics:nil views:views]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[v]|" options:0 metrics:nil views:views]];
[self.searchBar setBackgroundImage:[UIImage imageNamed:@"searchbarBG"] forBarPosition:UIBarPositionTopAttached barMetrics:UIBarMetricsDefault];
}
- (UIBarPosition)positionForBar:(id <UIBarPositioning>)bar
{
return UIBarPositionTopAttached;
}
Run Code Online (Sandbox Code Playgroud)
当我UISearchDisplayController通过在viewDidLoad方法的末尾附加以下行将搜索栏添加到a时出现问题:
self.mySearchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:self];
Run Code Online (Sandbox Code Playgroud)
这是我得到的结果:

请注意,图像看起来有所剪裁.这与我在代码中将UIBarPositionTopAttached更改为UIBarPositionTop时的情况不同:

被剪裁的图像让我想到了clipsToBounds,我可以通过在以下结尾添加这段代码来正确显示搜索栏viewDidLoad:
for (UIView *subview in self.searchBar.subviews) {
subview.clipsToBounds = NO;
} …Run Code Online (Sandbox Code Playgroud) 我有一个搜索栏的问题,当它变成a firstResponder并且它何时退出时,它会以一种奇怪的方式运行.
搜索栏将添加为表视图的标题
self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, self.view.frame.size.width, 44.0f)];
self.searchBar.translucent = NO;
self.searchBar.barTintColor = [UIColor grayColor];
self.tableView.tableHeaderView = self.searchBar;
self.searchController = [[UISearchDisplayController alloc] initWithSearchBar:self.searchBar
contentsController:self];
self.searchController.searchResultsDataSource = self;
Run Code Online (Sandbox Code Playgroud)
视图控制器设置为左侧面板,JASidePanelController当键盘显示或隐藏时,它会隐藏中央面板:
- (void)keyboardWillAppear:(NSNotification *)note
{
[self.sidePanelController setCenterPanelHidden:YES
animated:YES
duration:[[note.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue]];
self.searchBar.showsCancelButton = YES;
}
- (void)keyboardWillDisappear:(NSNotification *)note
{
[self.sidePanelController setCenterPanelHidden:NO
animated:YES
duration:[[note.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue]];
self.searchBar.showsCancelButton = NO;
}
Run Code Online (Sandbox Code Playgroud)
正常状态
当搜索栏变为a时,firstResponder它会向上移动一个点或随机向下移动

当搜索栏退出时,它会动画到达窗口原点,然后返回其自然帧


这是一个重现错误的示例项目.
编辑:
根据@kwylez的 建议,可以通过以下方式避免搜索栏在重新签名时产生的不需要的动画:
self.searchBar.clipsToBounds = YES;
Run Code Online (Sandbox Code Playgroud) 我有一个searchDisplayController搜索a UITableView.
输入搜索字词后,我可以看到UITableView包含搜索结果的其他字词.但是,我希望这UITableView是GROUPED,而不是PLAIN(默认情况下是这样).
我该怎么做呢?
如何在UISearchDisplayController的UISearchBar中更改"取消"按钮,"无结果"标签的字符串?
所以我有一个包含节和行的tableView,它使用自定义单元类.自定义单元格具有图像视图和一些标签.表视图工作正常,搜索工作,但搜索不显示我的自定义单元格类中的任何标签,只显示具有正确图像的imageView.我很困惑为什么会这样,特别是因为图像仍然显示,但不是标签.这是一些代码.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//TODO: problem with search view controller not displaying labels for the cell, needs fixing
JSBookCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if(cell == nil) {
cell = [[JSBookCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
JSBook *book = nil;
//uses the appropriate array to pull the data from if a search has been performed
if(tableView == self.searchDisplayController.searchResultsTableView) {
book = self.filteredTableData[(NSUInteger)indexPath.section][(NSUInteger)indexPath.row];
}
else {
book = self.books[(NSUInteger)indexPath.section][(NSUInteger)indexPath.row];
}
FFMetaData *data = [self.ff metaDataForObj:book];
cell.titleLabel.text = book.title;
cell.priceLabel.text …Run Code Online (Sandbox Code Playgroud) iphone objective-c uitableview uisearchdisplaycontroller ios
我最近更新了我的应用程序并遇到了这个问题.当我开始在搜索栏中输入时,搜索栏消失,我只能看到表格视图.我仍然可以继续打字,表格视图会更新但我看不到搜索栏.相同的设置在iOS <7上正常工作
知道为什么会这样吗?

我有一个UISearchBar在titleView导航栏(它看起来像下面的图片)的,我想使它的工作方式类似于一个在Safari浏览器.
我没有足够的声誉来发布图片,这是链接
第一张照片

第二张照片

我想要实现的目标是:
UISearchBar在该搜索栏内有一个清除按钮(看起来像带有2个加法按钮的第一张图片).view被另一个取代view,当它被解雇时,view返回到原始状态.我知道这可以通过使用来完成,UISearchDisplayController但它不起作用.这是我的代码:
.h:实施TableView's DataSource/Delegate, UISearchDisplayDelegate, UISearchBarDelegate
.M
-(void) viewDidload
{
//Add left, right buttons
self.leftButton = [[UIBarButtonItem alloc] init];
[self.leftButton setStyle:UIBarButtonItemStylePlain];
[self.leftButton setTitle:@"Button"];
self.navigationItem.leftBarButtonItem = self.leftButton;
self.rightButton = [[UIBarButtonItem alloc] init];
[self.rightButton setStyle:UIBarButtonItemStylePlain];
[self.rightButton setTitle:@"Button"];
self.navigationItem.rightBarButtonItem = self.rightButton;
self.searchBar = [[UISearchBar alloc] initWithFrame:self.navigationController.navigationBar.bounds];
if (DEVICE_IS_IOS7) {
self.searchBar.searchBarStyle = UISearchBarStyleMinimal;
}
self.navigationItem.titleView = self.searchBar;
self.searchBar.delegate = self; …Run Code Online (Sandbox Code Playgroud) 我有一个tableView从其中获取单元格内容CoreData并且已经用SearchDisplayController新的替换(已弃用)SearchController.我使用相同的tableView控制器来呈现对象的完整列表以及过滤/搜索的对象.
我设法使搜索/过滤工作正常,可以从过滤列表移动到这些项的详细视图,然后编辑并将更改成功保存回过滤的tableView.我的问题是滑动以从筛选列表中删除单元格导致运行时错误.之前SearchDisplayController我可以轻松地执行此操作,因为我可以访问SearchDisplayController's结果tableView,因此以下(伪)代码可以正常工作:
func controllerDidChangeContent(controller: NSFetchedResultsController) {
// If the search is active do this
searchDisplayController!.searchResultsTableView.endUpdates()
// else it isn't active so do this
tableView.endUpdates()
}
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,没有这样的tableView暴露给UISearchController我和我不知所措.我曾尝试制作tableView.beginUpdates()和tableView.endUpdates()条件上的tableView不被搜索的tableView但没有成功.
为了记录这是我的错误消息:
Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-3318.65/UITableView.m:1582
*编辑*
我的tableView使用FetchedResultsController从CoreData填充自己.这个tableViewController也是SearchController用来显示过滤结果的那个.
var searchController: UISearchController!
Run Code Online (Sandbox Code Playgroud)
然后在ViewDidLoad中
searchController = UISearchController(searchResultsController: nil)
searchController.dimsBackgroundDuringPresentation = false
searchController.searchResultsUpdater = self
searchController.searchBar.sizeToFit()
self.tableView.tableHeaderView = searchController?.searchBar
self.tableView.delegate = self
self.definesPresentationContext = true …Run Code Online (Sandbox Code Playgroud) uitableview uisearchdisplaycontroller ios swift uisearchcontroller
当用户将注意力集中在导航栏上搜索栏下方的搜索栏时,我正在尝试为UISearchController的"位置"输入添加另一个文本字段.
我拥有的例子以及我希望它去的地方:

我尝试过这样的东西,但是不起作用:
var searchController: UISearchController!
func searchBarTextDidBeginEditing(searchBar: UISearchBar) {
var myTextField: UITextField = UITextField(frame: CGRect(x: 0, y: 0, width: 200.00, height: 40.00))
myTextField.backgroundColor = UIColor.purpleColor()
myTextField.text = "Location"
myTextField.borderStyle = UITextBorderStyle.None
searchController.view.addSubview(myTextField)
}
Run Code Online (Sandbox Code Playgroud)
任何帮助都会很棒!谢谢.
uitextfield uisearchdisplaycontroller swift uisearchcontroller
ios ×7
uisearchbar ×4
iphone ×3
uitableview ×3
ios7 ×2
objective-c ×2
swift ×2
iso ×1
uitextfield ×1