iOS 7中UISearchBar的取消和清除按钮不起作用。

7
我有一个xCode项目,其中包含一个“搜索栏和搜索显示控制器”的表视图,允许用户细化显示项目列表。总体上,遵循了http://www.raywenderlich.com/16873/how-to-add-search-into-a-table-view中提供的指导。我最近下载了最新的xCode(版本5.0(5A1413)),支持iOS 7,并在不同的目标上测试了相关应用程序。

在iOS 6目标(模拟器或实际设备)上运行此应用程序时,按下取消按钮会按预期工作,这意味着搜索栏将被移除,按下清除按钮(小灰色x)将清除用户已输入的所有搜索条件。但是,当在iOS 7目标上运行该项目时,清除和取消按钮都无法正常工作。

在此项目中实现了searchBarCancelButtonClicked方法,并且我已经验证了在运行iOS 7的目标时未调用该方法。

- (void)searchBarCancelButtonClicked:(UISearchBar *)SearchBar
{
    NSLog(@"searchBarCancelButtonClicked called");

    self.searchBar.text = nil;

    …

    // Hide Search bar when cancelled
    [self hideSeachBar];

    [self.searchBar resignFirstResponder];

    …
    }

我的表视图控制器被设置为UISearchDisplayDelegate和UISearchBarDelegate。而且似乎这仍然有效,因为在iOS 6或7目标上调用searchBar:textDidChange:

@interface ItemViewController () <UISearchDisplayDelegate, UISearchBarDelegate>@end

我找不到其他与此相关的帖子或任何关于iOS 7变更的材料(如https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/TransitionGuide/Bars.html#//apple_ref/doc/uid/TP40013174-CH8-SW1),提到需要进行任何重新编码以支持iOS7。

您对此有什么想法吗?谢谢


我还没有机会查看答案,但我刚注意到同一问题的这些帖子:https://devforums.apple.com/message/880210#880210 和 https://devforums.apple.com/message/876407#876407。 - Mike
这段代码对于我的iOS7有效:https://dev59.com/YHfZa4cB1Zd3GeqPVLqi#22287788 - William Wong Garay
6个回答

3

我有同样的问题,我尝试了以下代码。请尝试这个。

-(void)searchDisplayControllerDidBeginSearch:(UISearchDisplayController *)controller
{
    controller.active = YES;

    [self.view addSubview:controller.searchBar];
    [self.view bringSubviewToFront:controller.searchBar];
}

- (void)searchDisplayController:(UISearchDisplayController *)controller didShowSearchResultsTableView:(UITableView *)tableView  {

    tableView.frame = self.archiveListTblView.frame;
}

- (void)searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller
{
    controller.active=NO;

    [self.view addSubview:controller.searchBar];
}

想知道你是如何设置和使用self.archiveListTblView的。 - Mike
你好,Mike。在我看来,iOS7自动为其本地窗口创建布局,这就是我们所面临问题的原因。另外,我已尝试使用“self.view.frame”代替“self.archiveListTblView.frame”,即使这个代码对我来说也可以正常工作。所以,问题在于我们没有为表格视图设置框架。:-P - Prabakaran

1
这个问题似乎是由导航栏中半透明属性的新行为引起的。
自从iOS 7以来,导航栏默认是半透明的。当你按下一个按钮后显示搜索栏时,它看起来会重叠在导航栏上。如果你滚动到listView的顶部并使用搜索栏,则应该可以正常工作。
尝试在你的控制器中设置:
float osVersion = [[[UIDevice currentDevice] systemVersion] floatValue];
if (osVersion >= 7.0)
{
    self.navigationController.navigationBar.translucent = NO;
}

这应该能够快速解决问题。

但我认为为了更好的修复,您应该查看iOS 7 过渡指南,其中他们解释了如何处理半透明导航栏。

希望这有所帮助。


1

我也遇到了这个问题。奇怪的是,UISearchBarDelegate 的其他委托方法正在被调用。一个解决方法可能是:

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
    if ([searchText length] == 0) {
        NSLog("No Text");
    }
}

它对我起作用了


1

如果您在导航栏上点击搜索按钮,则取消按钮和清除按钮将无法使用。如果您点击搜索栏开始搜索,则它可以正常工作。 => 这意味着,如果在开始搜索之前屏幕上不可见搜索栏,则这些按钮似乎也会被禁用。

因此,我找到了一个解决方案,但它并不完美。 在使搜索栏成为第一响应之前,您必须将表视图滚动到顶部。尝试使用此代码。

-(IBAction)goToSearch:(id)sender {
     // Make search bar visible on screen before make it response
    [_tableView setContentOffset:CGPointMake(0, 0) animated:NO];

    // Make search bar active
    [candySearchBar becomeFirstResponder];
}

如果你这样做,唯一的问题是你的表视图会滚动到顶部,这意味着当你取消搜索时,你会失去当前单元格索引。

0
在我的情况下,我已经将搜索栏重新定位到屏幕顶部,并且有一个不可见的视图覆盖在搜索栏上方。
因此,取消按钮实际上没有被触摸。
所以,当调用[searchBarTextDidBeginEditing]方法时,我已经将搜索栏置于前台,如下所示。
-(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar{
    lc_SearchBarYPos.constant = 20.0f; //this is code to reposition the searchbar
    [self.view bringSubviewToFront:searchBar];
}

希望这可以帮到你。


0

我在互联网上搜索了很久,但是找不到解决方案。所以我改变了UItableview的行为。

我不再使用[searchBar becomeFirstResponder];,而是滚动TableView。

- (IBAction)goToSearch:(id)sender {

 scroll down to show the table.
//    CGRect newBounds = self.TableView.bounds;
//    newBounds.origin.y =0;
//    
//    self.TableView.bounds = newBounds;
//[searchBar becomeFirstResponder];

    CGPoint contentOffset=self.TableView.contentOffset;
    contentOffset.y=0;
    [self.TableView setContentOffset:contentOffset animated:YES];


}

在我的ViewDidload中:

//        CGRect newBounds = self.TableView.bounds;
//        newBounds.origin.y = newBounds.origin.y + searchBar.bounds.size.height;
        // self.TableView.bounds = newBounds;

        CGPoint contentOffset=self.TableView.contentOffset;
        contentOffset.y=self.TableView.bounds.origin.y + searchBar.bounds.size.height;
        self.TableView.contentOffset=contentOffset;

如果在iOS 7中发现某些情况下,更改表视图边界会导致搜索栏消失。 希望这可以帮到你。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接