UISearchController好用,但是要避过这些坑

1.搜索时searchBar向上偏移64

设置definesPresentationContext为YES,可以保证在UISearchController在激活状态下用户push到下一个view controller之后search bar不会仍留在界面上。

a、如果不添加上面这行代码,在设置hidesNavigationBarDuringPresentation这个属性为YES的时候,搜索框进入编辑模式会导致,searchbar向上偏移了64;

b、如果添加了上面这行代码,在设置hidesNavigationBarDuringPresentation这个属性为YES的时候,输入框进入编辑模式正常显示和使用;

2.搜索返回时searchBar向下偏移64

a、在definesPresentationContext为YES和hidesNavigationBarDuringPresentation都设置为YES的时候,打开下一个页面,再返回,searchbar向下偏移64,这种情况可能有的人不会遇到,有的人会遇到,具体来说就是ios11之后的人不会遇到

解决方法:navigationBar的translucent属性

if(@available(iOS11.0, *)) {

        self.navigationController.navigationBar.translucent = NO;

    }else{

        self.navigationController.navigationBar.translucent = YES;

    }

3.用UISearchController做的搜索框  ,更换背景颜色后 ,上下有两条黑色的线

UISearchBar*searchBar =self.searchController.searchBar;

UIImageView*barImageView = [[[searchBar.subviewsfirstObject]subviews]firstObject];

barImageView.layer.borderColor= [UIColor grayColor].CGColor;

 barImageView.layer.borderWidth=1;

self.tableView.tableHeaderView = searchBar;

3.searchBar加到tableView的headerView上,然后为tableView添加sectionIndex,tableView整体左移,searchBar也一样

解决方法:

UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 210)];

view.backgroundColor= [UIColor whiteColor];

[view addSubview:searchBar]

self.tableView.tableHeaderView = view;


©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容