开发者

UITableView how to "dim" the actual table when UISearchbar gets focus?

开发者 https://www.devze.com 2022-12-19 05:28 出处:网络
Many of the table searches I see actually dim (change alpha?) of the actual tableView when the search bar gets focused. I am trying to implement this开发者_StackOverflow社区.

Many of the table searches I see actually dim (change alpha?) of the actual tableView when the search bar gets focused. I am trying to implement this开发者_StackOverflow社区.

When I overload

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar

I am trying to do something like

self.tableView.alpha = .5f

But it is changing the alpha of the actual searchBar (if I have searchBar on its own) or changing the alpha of the entire table (including searchBar) if I have searchBar underneath the tableView in IB.

What am I doing wrong here. How can I just Dim the table and not everything.

I guess what I am really failing to understand is how all this stuff gets layered on the screen.

EDIT: The contacts application is a good example of what I am trying to do.


Most such apps just throw a black-background UIView on top of the table and fade it in (to an alpha of .5 or whatever) when the search bar gains focus.


If you use UISearchDisplayController, you get this behavior for free.


Since iOS 8 (and up to at least iOS 9.2) you would use a UISearchController. Unfortunately, currently the interface builder supports only the now deprecated UISearchDisplayController. So, this is how to add it programmatically to your table view controller, including the dim effect:

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号