7

当搜索栏获得焦点时,我看到的许多表格搜索实际上是实际 tableView 的暗淡(更改 alpha?)。我正在尝试实现这一点。

当我超载

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar

我正在尝试做类似的事情

self.tableView.alpha = .5f

但它正在更改实际 searchBar 的 alpha(如果我有自己的 searchBar)或更改整个表(包括 searchBar)的 alpha,如果我在 IB 的 tableView 下有 searchBar。

我在这里做错了什么。我怎么能只调暗桌子而不是所有东西。

我想我真正无法理解的是所有这些东西是如何在屏幕上分层的。

编辑:联系人应用程序是我正在尝试做的一个很好的例子。

4

3 回答 3

10

大多数这样的应用程序只是在表格顶部抛出一个黑色背景的 UIView 并在搜索栏获得焦点时将其淡入(到 0.5 的 alpha 或其他)。

于 2010-02-07T22:01:23.463 回答
2

如果您使用UISearchDisplayController,您将免费获得此行为。

于 2012-04-10T22:22:24.783 回答
-1

从 iOS 8(以及至少 iOS 9.2)开始,您将使用UISearchController. 不幸的是,目前界面构建器仅支持现在已弃用的UISearchDisplayController. 因此,这是如何以编程方式将其添加到您的表格视图控制器,包括暗淡效果:

let searchController = UISearchController(searchResultsController: nil)

override func viewDidLoad() {
    super.viewDidLoad()

    // Use the current view controller to update the search results:
    self.searchController.searchResultsUpdater = self
    self.searchController.dimsBackgroundDuringPresentation = true
    self.tableView.tableHeaderView = searchController.searchBar
}
于 2016-02-19T22:32:24.350 回答