如何用动画隐藏/显示 UISearchBar 的范围栏?

发布于 2024-09-05 23:09:50 字数 500 浏览 3 评论 0原文

我想在表格为空时(在搜索栏第一次编辑之前)不显示范围栏,在编辑时不显示范围栏,最后在编辑完成时显示它。我了解 UISearchBarDelegate 协议,但我不知道如何用动画显示/隐藏范围栏。我知道 UISearchBar 有 setShowsScopeBar:,但没有 setShowsScopeBar:animated:,就像 setShowsCancelButton:animated 那样。

编辑 在显示/隐藏范围栏后调用 [searchBar sizeToFit] 很重要。有没有一个好的方法来制作动画? (我应该这样做 这似乎不起作用。)

I want to show no scope bar when the table is empty (before the search bar edits for the first time), no scope bar when it's editing, and finally show it when editing done. I know about the UISearchBarDelegate protocol, but I don't know how to show/hide the scope bar with animation. I know UISearchBar has setShowsScopeBar:, but no setShowsScopeBar:animated: the way it does for setShowsCancelButton:animated.

Edit
It's important that to call [searchBar sizeToFit] after showing/hiding the scope bar. Is there a good way to animate this? (Should I do this? It doesn't appear to work.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

嘿咻 2024-09-12 23:09:50

以下是如何使取消按钮和范围栏仅在编辑时显示。

完整的代码,带有取消按钮(取消)外观动画的小好处:

- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
    searchBar.showsScopeBar = YES;
    [searchBar sizeToFit];
    [searchBar setShowsCancelButton:YES animated:YES];

    return YES;
}

- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar {
    searchBar.showsScopeBar = NO;
    [searchBar sizeToFit];
    [searchBar setShowsCancelButton:NO animated:YES];

    return YES;
}

编辑 - Swift 3 版本

public func searchBarShouldBeginEditing(_ searchBar: UISearchBar) -> Bool {
    searchBar.showsScopeBar = true
    searchBar.sizeToFit()
    searchBar.setShowsCancelButton(true, animated: true)

    return true
}

public func searchBarShouldEndEditing(_ searchBar: UISearchBar) -> Bool {
    searchBar.showsScopeBar = false
    searchBar.sizeToFit()
    searchBar.setShowsCancelButton(false, animated: true)

    return true
}

来源:http://www.alexandre-gomes.com/?p=418

Here's how to make the cancel button and the scope bar to be displayed only while editing.

Complete code with the small bonus of animating the cancel button’s (dis)appearance:

- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
    searchBar.showsScopeBar = YES;
    [searchBar sizeToFit];
    [searchBar setShowsCancelButton:YES animated:YES];

    return YES;
}

- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar {
    searchBar.showsScopeBar = NO;
    [searchBar sizeToFit];
    [searchBar setShowsCancelButton:NO animated:YES];

    return YES;
}

EDIT - Version Swift 3

public func searchBarShouldBeginEditing(_ searchBar: UISearchBar) -> Bool {
    searchBar.showsScopeBar = true
    searchBar.sizeToFit()
    searchBar.setShowsCancelButton(true, animated: true)

    return true
}

public func searchBarShouldEndEditing(_ searchBar: UISearchBar) -> Bool {
    searchBar.showsScopeBar = false
    searchBar.sizeToFit()
    searchBar.setShowsCancelButton(false, animated: true)

    return true
}

Source: http://www.alexandre-gomes.com/?p=418

執念 2024-09-12 23:09:50

对于较新的 iOS 版本(在 11-13 上测试),您不必实现任何委托方法即可使其工作。

在 iOS 13 中,默认行为是始终显示范围栏,即使当搜索未激活时。以前的 iOS 版本在搜索处于活动状态时会自动显示和隐藏范围栏。

您需要将其添加到 viewDidLoad()

if #available(iOS 13.0, *) {
    searchController.automaticallyShowsScopeBar = true
}

For the newer iOS versions (tested on 11-13), you don't have to implement any delegate methods for this to work.

In iOS 13 the default behaviour is to always show the scope bar, even when search is not active. Previous iOS versions automatically show and hide the scope bar when search is active.

You need to add this in viewDidLoad()

if #available(iOS 13.0, *) {
    searchController.automaticallyShowsScopeBar = true
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文