iPhone x searchBar编辑状态黑线问题
2018-03-01 本文已影响39人
落寒z
我在适配iPhone x的时候,我修改了searchBar的背景颜色后,在编辑状态总是出现一条黑线

未处理背景色的时候代码:
//设置搜索框
UISearchBar *searchBar = _searchController.searchBar;
// 设置搜索框外部的颜色
searchBar.backgroundImage = [UIImage imageWithColor:AB_White_Color size:searchBar.frame.size];
// 处理搜索框编辑状态时的背景色
CGSize size = CGSizeMake(searchBar.frame.size.width, searchBar.frame.size.height + 20);
[searchBar setBackgroundImage:[UIImage imageWithColor:AB_White_Color size:size] forBarPosition:UIBarPositionTopAttached barMetrics:UIBarMetricsDefault];
searchBar.showsCancelButton = NO;
[searchBar sizeToFit];
searchBar.placeholder = NSLocalizedString(@"public.search", nil);
searchBar.delegate = self;
searchBar.barTintColor = [UIColor whiteColor]; //调整iPhone x上背景颜色的改变
经测试后,将UIBarMetricsDefault修改为UIBarMetricsDefaultPrompt即可
其他机型上还是使用UIBarMetricsDefault
解决黑线问题
UIBarMetrics barMetrics = UIBarMetricsDefault;
if (IPHONE_X) {
barMetrics = UIBarMetricsDefaultPrompt;
}
[searchBar setBackgroundImage:[UIImage imageWithColor:AB_White_Color size:size] forBarPosition:UIBarPositionTopAttached barMetrics:barMetrics];
效果图:
