【问题标题】:How to show/hide a search bar inside a navigation bar (iOS 7) as in Apple's Calendar app?如何在 Apple 的日历应用程序中显示/隐藏导航栏 (iOS 7) 内的搜索栏?
【发布时间】:2014-02-13 12:50:37
【问题描述】:

我想使用导航栏中的搜索栏按钮来显示搜索栏,就像 Apple 在日历应用中所做的那样。取消按钮将关闭搜索栏并将导航栏恢复到以前的状态、栏按钮、标题等。

使用 iOS 7 属性:

self.searchDisplayController.displaysSearchBarInNavigationBar = YES;

把搜索栏放到导航栏就好了。我的问题是试图让它在按下条形按钮时有条件地出现。我试过从按钮的动作中触发这条线,但没有成功。我在 searchDisplayController 上尝试过 setActive:Animated:

self.searchDisplayController.active = YES;

但也没有运气。任何想法或帮助将不胜感激。

【问题讨论】:

    标签: ios iphone objective-c search navigationbar


    【解决方案1】:

    我不确定你是否注意到,但在 Apple 的日历应用中,当你按下搜索图标时,它会打开一个带有搜索栏的新 UITableView。如果这是您想要做的,您将创建一个带有 UITableView 和 UISearchBar 的 UIViewController,在该 tableView 中您将过滤内容。

    如果我是你,我只会隐藏 UISearchBar 并在需要时调用它并显示按钮。

    这也可能有效。试一试,然后告诉我:

    在你看来会出现:

    - (void)viewWillAppear:(BOOL)animated {
    
    // scroll search bar out of sight
    CGRect newBounds = self.tableView.bounds;
    if (self.tableView.bounds.origin.y < 44) {
        newBounds.origin.y = newBounds.origin.y + self.searchBar.bounds.size.height;
        self.tableView.bounds = newBounds;
    }
    // new for iOS 7
    [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0] atScrollPosition:0 animated:YES];}
    

    现在它被隐藏了,当搜索完成时调用它再次隐藏它:

    - (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
        [self viewWillAppear:YES];
    }
    

    然后用按钮显示 searchBar,然后:

    - (IBAction)displaySearchBar:(id)sender {
        // makes the search bar visible
        [self.searchBar becomeFirstResponder];
    }
    

    【讨论】:

    • 谢谢。允许我使用 searchDisplayController 我认为这可能是一个很好的方法。
    【解决方案2】:

    在 iOS 8 中,您可以简单地呈现 UISearchController,您将获得与 Calendar.app 相同的动画。查看 Apple 的 UICatalog 示例代码以获取示例。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多