【问题标题】:iOS 7 Search Bar - Cancel button doesn't workiOS 7 搜索栏 - 取消按钮不起作用
【发布时间】:2014-02-09 14:35:28
【问题描述】:

我在 iOS 6 应用程序中有一个 UISearchBar,它运行良好,但在 iOS 7 中,取消按钮和清除按钮不起作用,我无法返回。这是我的应用程序中的一个大问题,我需要解决它。

我的代码是:

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString

{
    // -- iOS 7 Hack

    if (!SYSTEM_VERSION_LESS_THAN(@"7.0")) {
        controller.searchResultsTableView.frame = CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height-64);
        [controller.searchContentsController.view setNeedsLayout];
    }

    [self filterContentForSearchText:searchString scope:nil];
    return YES;


}

- (void) searchDisplayController:(UISearchDisplayController *)controller didShowSearchResultsTableView:(UITableView *)tableView {
    // iOS7 Hack
    if (!SYSTEM_VERSION_LESS_THAN(@"7.0")) {
        controller.searchResultsTableView.contentInset = UIEdgeInsetsMake(0.f, 0.f, 0.f, 0.f);
    }

}

感谢您的提前。

可能重复:UISearchBar's Cancel and Clear Buttons Not Working in iOS 7

编辑:

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    self.searchDisplayController.searchBar.hidden = YES;
    self.tempImageView.hidden = NO;
    [searchBar resignFirstResponder];
}

解决方案:

通过这个功能我解决了这个问题:

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

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

希望对你有帮助!

【问题讨论】:

    标签: ios objective-c ios7 uisearchbar


    【解决方案1】:

    IOS 7 默认搜索栏将是透明的取消按钮

    - (void)searchBarCancelButtonClicked:(UISearchBar *)aSearchBar
    {
        [aSearchBar resignFirstResponder];
        isSearching = NO;
        aSearchBar.text = @"";
    
        [diaryTableView reloadData];
    
    }
    

    【讨论】:

      【解决方案2】:

      通过这个功能我解决了这个问题:

      -(void)searchDisplayControllerDidBeginSearch:(UISearchDisplayController *)controller
      {
          controller.active = YES;
      
          [self.view addSubview:controller.searchBar];
          [self.view bringSubviewToFront:controller.searchBar];
      }
      

      希望对你有帮助!

      【讨论】:

      • 如果controller 不活跃,为什么还要调用searchDisplayControllerDidBeginSearch:
      • 控制器开始搜索时调用。
      • 按下取消按钮时调用什么方法?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-26
      • 1970-01-01
      • 1970-01-01
      • 2017-04-05
      相关资源
      最近更新 更多