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

时间:2013-10-03 13:17:18

标签: ios objective-c ios7 uisearchbar

我有一个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目标上运行时,clear和cancel按钮都不起作用。

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。看起来这仍然适用于searchBar:textDidChange:在iOS 6或7目标上调用。

@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而需要进行的任何重新编码。

对此有何想法?感谢

6 个答案:

答案 0 :(得分: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];
}

答案 1 :(得分:1)

按钮取消和按钮清除将无效,如果您触摸按钮搜索导航。如果您点按搜索栏以进行星标搜索,它将正常运行 =&GT;这意味着如果在开始搜索之前在屏幕上看不到搜索栏,那么这些按钮看起来也是禁用的。

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

-(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];
}

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

答案 2 :(得分:1)

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

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

尝试在控制器中设置:

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

这应该很快解决问题。

但我认为为了更好的解决方案,您应该看到iOS 7 transition guide解释如何处理半透明导航栏的问题。

希望有所帮助。

答案 3 :(得分:1)

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

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

它对我有用

答案 4 :(得分:0)

我在互联网上搜索,无法找到解决方案。所以我改变了UItableview行为。

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

- (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中,更改表视图边界会导致搜索栏消失。 希望有所帮助。

答案 5 :(得分:0)

在我的情况下,我将搜索栏重新定位到屏幕顶部,并且有一个不可见的视图重叠在搜索栏上。

因此实际上没有触及取消按钮。

所以,当调用[searchBarTextDidBeginEditing]方法时,我已经把seachbar带到了前面。

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

希望这可能会有所帮助。