UISearchController's search bar overlaps first tableview cell

谁都会走 提交于 2019-12-04 16:33:33

问题


I am using UISearchController and the UISearchBar also has a scope bar. The search bar is added as a subview to a UIView which is above the UItableView. I have done it this way since I want the search bar to be always visible even when the tableview is scrolled.

The problem is the scopebar overlaps the first tableview cell

StoryBoard

Scope bar overlapping the tableview cell

How can I prevent this overlapping?, I can't display the searchbar in navigationbar since the scopebar when placed in navigation bar does not get displayed.


回答1:


This worked for me:

Having the Search Display Controller and SearchBar in the tableview header. Add the heightForHeaderInSection in your TableViewController.

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 44; //I used the 44, the height of SearchBar
}

In your case you'll need to also add the scope bar height. Now it will always keep a base height.




回答2:


This worked for me :

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (self.searchController.active) {
        return 44; // with scope
    } else {
        return 0; // no scope
    }
}



回答3:


Add the search bar and table view on the UIViewController as shown in the image below. Don't overlap the search bar with the table view. I am using this in my app and its working fine for me.



来源:https://stackoverflow.com/questions/29264571/uisearchcontrollers-search-bar-overlaps-first-tableview-cell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!