你如何用动画隐藏/显示UISearchBar的范围栏? [英] How do you hide/show UISearchBar's scope bar with animation?

查看:169
本文介绍了你如何用动画隐藏/显示UISearchBar的范围栏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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.)

推荐答案

以下是取消按钮和范围的方法条形图仅在编辑时显示。

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

完整代码,带有取消按钮(dis)外观动画的小额奖励:

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;
}

编辑 - 版本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

这篇关于你如何用动画隐藏/显示UISearchBar的范围栏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆