【问题标题】:searchBar does not hide after pushing another view controller推送另一个视图控制器后 searchBar 不隐藏
【发布时间】:2015-02-25 15:03:04
【问题描述】:

我在表格的标题视图中创建了一个搜索栏:

self.searchResults = [NSMutableArray arrayWithCapacity:[self.list count]];
self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
self.searchController.searchResultsUpdater = self;
self.searchController.dimsBackgroundDuringPresentation = NO;
self.searchController.hidesNavigationBarDuringPresentation = NO;
self.searchController.searchBar.barTintColor = [UIColor clearColor];
self.searchController.searchBar.frame = CGRectMake(self.searchController.searchBar.frame.origin.x, self.searchController.searchBar.frame.origin.y, self.searchController.searchBar.frame.size.width, 44.0);
[[UIBarButtonItem appearanceWhenContainedIn: [UISearchBar class], nil] setTintColor:[UIColor whiteColor]];
[self.searchController.searchBar becomeFirstResponder];

并且当点击 tableview 中的某一行时,将推送一个新的视图控制器。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSLog(@"HEY ROW %ld", indexPath.row+1);
    ZLContainerViewController *controller = [[ZLContainerViewController alloc]init]; // a container for UIPage
    [self.navigationController pushViewController:controller animated:YES];    
}

但是,在推送新的视图控制器后,搜索栏仍然存在。我该如何解决这个问题?

非常感谢任何建议!

【问题讨论】:

  • 您正在创建一个 UISearchController 。尝试创建一个 UISearchBar

标签: ios objective-c iphone uitableview uisearchbar


【解决方案1】:

viewDidLoad中使用下面的代码。

斯威夫特:

self.definesPresentationContext = true

目标-C:

self.definesPresentationContext = YES;

这解决了我的问题。

【讨论】:

    【解决方案2】:

    这两个 UISearchBarDelegate 委托方法之一应该可以解决您的问题

    - (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar {
    return YES;
    }
    
    - (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
    [self.searchBar resignFirstResponder];
    }
    

    【讨论】:

    • 感谢您的评论!虽然这种方法并没有直接解决我的问题,但是“resignFirstResponder”给了我一个提示。我最终调用了“resignFirstResponder”并将搜索栏更改为隐藏,每当推送新的视图控制器时。再次感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-03
    • 1970-01-01
    • 1970-01-01
    • 2023-03-24
    • 1970-01-01
    • 2014-03-28
    相关资源
    最近更新 更多