关于C#:UISearchBar的“取消”和“清除”按钮在iOS 7中不起作用

UISearchBar's Cancel and Clear Buttons Not Working in iOS 7

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

在iOS 6目标(仿真器或真实设备)上运行此应用程序时,它可以按预期工作,这意味着按"取消"按钮将删除搜索栏,而按"清除"按钮(小灰色x)将清除由键入的所有搜索条件。用户。但是,当项目在iOS 7目标上运行时,清除和取消按钮均不起作用。

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
- (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:。

1
2
3
@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。

有什么想法吗?谢谢


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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
-(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];
}


我也有这个问题。奇怪的是,其他UISearchBarDelegate的委托方法也被调用。解决方法可能是:

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

对我有用


此问题似乎来自导航栏中半透明属性的新行为。

由于iOS 7导航栏默认为半透明。当您按下按钮后将其显示时,它似乎与搜索栏重叠。如果滚动到listView的顶部并使用搜索栏,则它应该可以正常工作。

尝试设置您的控制器:

1
2
3
4
5
float osVersion = [[[UIDevice currentDevice] systemVersion] floatValue];
if (osVersion >= 7.0)
{
    self.navigationController.navigationBar.translucent = NO;
}

这应该很快解决问题。

但是我认为,要想获得更好的修复,您应该查看iOS 7过渡指南,其中介绍了如何处理半透明导航栏。

希望能有所帮助。


如果您触摸导航按钮,则"取消"按钮和"清除"按钮将不起作用。如果您点击搜索栏以进行星标搜索,则它可以正常工作
=>这意味着如果在开始搜索之前在屏幕上看不到搜索栏,那么这些按钮也似乎被禁用。

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

1
2
3
4
5
6
7
-(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];
}

如果执行此操作,唯一的问题是表格视图将滚动到顶部,这意味着当您取消搜索时,之前丢失了当前单元格索引。


就我而言,我已经将搜索栏重新放置在屏幕顶部,并且有一个不可见的视图重叠在搜索栏上。

因此取消按钮实际上未被触摸。

因此,当像下面这样调用[searchBarTextDidBeginEditing]方法时,我已经把seachbar放在了前面。

1
2
3
4
-(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar{
    lc_SearchBarYPos.constant = 20.0f; //this is code to reposition the searchbar
    [self.view bringSubviewToFront:searchBar];
}

希望这会有所帮助。


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

而不是[searchBar becomeFirstResponder];,我向下滚动表格视图。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
- (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中:

1
2
3
4
5
6
7
//        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中,更改表视图范围会导致搜索栏消失。
希望能有所帮助。