最近搞ReactNative去了,很久没写iOS原生。最近又写了点,在写位置POI搜索功能,突然发现UISearchDisplayController被废弃了,用UISearchController替代,OJBK就替代,结果遇到个坑,卡了两天。特此记录造福遇到同样问题的伙伴。
搜索滑动后返回,位置偏移了20像素(二十年单身狗钛合金眼扫描),我首先怀疑我在searchVC也用了MJRefreshFooter导致的,然而并不是,又设置autoadjust等都不是。各种尝试都失败,让我有点怀疑人生,难道半年没写OC就被淘汰了?
后来我各种百度无果。今早顺手打开Google一下tableviewheader searchBar 20。IOS -UISearchController UISearchBar这篇给了我答案,在此吐槽下某度,我用文章名都找不到,还是Google厉害。
扯远了,说下解决方法:偏移二十,在searchController的willPresent和willDismiss控制20的移动就可以了。例如:
// 修复searchBar偏移问题
#pragma mark -searchControllerDelegate
- (void)willDismissSearchController:(UISearchController *)searchController {
CGRect tableFrame = self.tabV.frame;
tableFrame.origin.y = 64;
tableFrame.size.height = self.view.frame.size.height -20-64;
self.tabV.frame = tableFrame;
[UIView animateWithDuration:0.4 animations:^{
[self.view layoutIfNeeded];
[self.tabV layoutIfNeeded];
}];
}
解决了我的SearchBar上移20问题(64是上面状态栏+按钮的高度)。上移同理~ 结果如下图