【问题标题】:Hiding search bar of UISearchDisplayController隐藏 UISearchDisplayController 的搜索栏
【发布时间】:2015-10-13 04:26:48
【问题描述】:

我有一个带有一些自定义的UISearchBar,我创建了一个这样的 UISearchDisplayController

self.searchController = [[UISearchDisplayController alloc]initWithSearchBar:self.searchBar contentsController:self];

我希望搜索栏出现在导航栏上,所以我也设置了

self.searchDisplayController.displaysSearchBarInNavigationBar = true;

现在搜索栏显示在导航栏中,但我只想在点击导航栏按钮项时显示UISearchDisplayController 的搜索栏。我想要这样的行为:

  1. 最初隐藏搜索栏
  2. 点击导航栏按钮时显示搜索栏
  3. 点击“取消”按钮时隐藏搜索栏 搜索栏

我试图像这样隐藏/取消隐藏它:

self.searchDisplayController.searchBar.hidden = YES;

但代码似乎不起作用。我花了很多时间寻找解决方案以获得我想要的行为,但仍然没有运气。谢谢。

【问题讨论】:

  • 您需要完成三项任务。这些都有效吗?
  • 这些步骤都不起作用,因为我还没有找到隐藏/取消隐藏UISearchDisplayController的搜索栏的方法。
  • 您是否满足于让搜索栏滚出屏幕(非动画)并在顶部导航栏下方,点击导航栏将其收回以显示下方的搜索栏(动画),点击搜索取消栏将导航栏恢复到搜索栏顶部(动画)?

标签: ios objective-c uisearchbar uisearchdisplaycontroller


【解决方案1】:

试试这个:

CGRect searchFrame = self.searchDisplayController.searchBar.frame;
searchFrame.size.height = 0;
self.searchDisplayController.searchBar.frame = searchFrame;
self.searchDisplayController.searchBar.hidden = YES;

编辑:我刚刚尝试使用下面的代码并且它有效。看看这对你有没有帮助!

- (void)viewDidLoad {
    [super viewDidLoad];

    [self.searchDisplayController.searchContentsController.navigationController setNavigationBarHidden:YES animated:YES];
    [self performSelector:@selector(test) withObject:nil afterDelay:2.0];
}


- (void)test {
    [self.searchDisplayController.searchContentsController.navigationController setNavigationBarHidden:NO animated:YES];
}

【讨论】:

  • 我尝试在viewDidLoadviewDidAppear 中添加此代码。但搜索栏仍在显示。
  • 我看到你正在传递self.seachbarself.searchbar.hidden = YES 怎么样?
  • 那也不行。设置 self.searchDisplayController.displaysSearchBarInNavigationBar = true; 后,隐藏 UISearchDisplayControllersearchBar 似乎不起作用?
  • 我需要找出答案。顺便说一句...如果您不支持 iOS UISearchController 来代替。
  • 我支持 iOS 7。这就是为什么。
【解决方案2】:

也许这是一个合适的解决方案?

要完成任务 1,在您的 TVC 生命周期方法 viewDidLoad 中,插入一个非动画滚动,将搜索栏置于导航栏下方...

- (void)viewDidLoad {
    [super viewDidLoad];

    // Scroll off screen the search bar (44 points)
    [[self tableView] setContentOffset:CGPointMake(0, 44)];

    // other code for method
} 

要完成任务 2,请在您的 TVC 中创建与 UIBarButtonItem 关联的自定义操作方法,该方法有效地隐藏导航栏,同时显示搜索栏...

- (IBAction)hideNavBar:(UIBarButtonItem *)sender {

    [[self navigationController] setNavigationBarHidden:YES animated:YES]

} 

要完成任务 3,在您的 TVC 中使用UISearchDisplayController Delegate 方法来有效显示导航栏并同时隐藏搜索栏...

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {

    [[self navigationController] setNavigationBarHidden:NO animated:YES];
    [[self tableView] setContentOffset:CGPointMake(0, 44)];

    // You might also like to...
    [self setSearchBarText:nil]; // if you are using a property to hold the search bar text
    [self setSearchResults:nil]; // if you are using a property to hold the search results

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-04
    • 2011-02-18
    • 2019-05-21
    • 1970-01-01
    • 2011-03-08
    • 1970-01-01
    相关资源
    最近更新 更多