【问题标题】:iOS search bar displaceiOS 搜索栏取代
【发布时间】:2014-05-16 12:12:04
【问题描述】:

我正在开发具有以下屏幕的 iPad (iOS7) 应用程序。在地图的右上角,我添加了 UITableView 和一个搜索栏和搜索显示。但是当我尝试在搜索栏上输入时,它只是移动到顶部导航栏,如第二个屏幕截图所示。我正在使用故事板来开发这些界面。我想将搜索栏保留在表格视图中。你能帮我解决这个问题吗?

1) 初始视图

2) 刚开始在搜索栏上输入

谢谢

【问题讨论】:

    标签: ios uitableview ios7 uisearchbar uistoryboard


    【解决方案1】:

    这可能是因为您使用的是 UISerchDisplayController,它是一种实现搜索功能的简单方法,但使用自定义界面会很痛苦。
    在我看来,您自己管理搜索会更好。或者您可以尝试使用UISearchDisplayDelegate,但在 iOS7 上,我在尝试根据应用设计更改 searchTableView 时遇到了很多主要问题。
    在前两种方法中,我尝试更改从搜索显示控制器返回的搜索 tvc 的框架。在 iOS7 中这段代码几乎没用,他们在内部改变了一些东西。

    #pragma mark -
    #pragma mark UISearchDisplayController Delegate Methods
    
    - (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller{
    
        NSLog(@"Old frame %@",NSStringFromCGRect(oldTableViewFrame));
        [UIView beginAnimations:@"" context:NULL];
        [UIView setAnimationDuration:1.5];
        self.receiptsTableView.frame= self.view.bounds;
        [UIView commitAnimations];
    
    }
    - (void)searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller{
        [UIView beginAnimations:@"" context:NULL];
        [UIView setAnimationDuration:1.5];
        self.receiptsTableView.frame= CGRectMake(self.view.bounds.origin.x, self.view.bounds.origin.x+self.imageContentView.bounds.size.height, self.view.bounds.size.width,  self.view.bounds.size.height-self.imageContentView.bounds.size.height);
        [UIView commitAnimations];
    }
    
    
    - (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
    {
        [self filterContentForSearchText:searchString scope: (NSString*)scopes[[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
    
        // Return YES to cause the search result table view to be reloaded.
        return YES;
    }
    
    
    - (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption
    {
        [self filterContentForSearchText:[self.searchDisplayController.searchBar text] scope:scopes[searchOption]];
    
        // Return YES to cause the search result table view to be reloaded.
        return YES;
    }
    

    【讨论】:

    • 能否提供您编写的任何示例代码?
    • 你是对的。如果我只添加搜索栏,那么我没有这个问题。我将尝试实现其余的。
    • 从我的一个旧应用程序中添加了代码,它改变了搜索 tvc 框架,但在 iOS7 中不再起作用了。
    • 我认为推荐的方法是使用[UIView animateWithDuration:(NSTimeInterval) animations:^(void)animations completion:^(BOOL finished)completion]
    【解决方案2】:

    关键是当您触摸UISearchBar 时,它的默认行为会启动(向左移动,添加取消按钮...)

    所以你必须覆盖它。

    这不是一个完美的解决方案,但您可以使用 UISearchBarDelegate 并实现这些方法:

    searchBarShouldBeginEditing:searchBarDidBeginEditing:(我不确定,你必须同时尝试)

    UISearchDisplayDelegate 与:

    – searchDisplayControllerWillBeginSearch:
    – searchDisplayControllerDidBeginSearch:
    
    – searchDisplayControllerWillEndSearch:
    – searchDisplayControllerDidEndSearch:
    

    当用户开始和结束搜索时,将其框架设置到您想要的位置。

    【讨论】:

    • 你知道我必须在搜索栏上覆盖哪个属性吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-16
    • 1970-01-01
    • 2018-03-12
    相关资源
    最近更新 更多