iPhone x searchBar编辑状态黑线处理


我在适配iPhone x的时候,我修改了searchBar的背景颜色后,在编辑状态总是出现一条黑线
image.png

未处理背景色的时候代码:

1
//设置搜索框
2
 UISearchBar *searchBar = _searchController.searchBar;
3
 // 设置搜索框外部的颜色
4
 searchBar.backgroundImage = [UIImage imageWithColor:AB_White_Color size:searchBar.frame.size];
5
 // 处理搜索框编辑状态时的背景色
6
 CGSize size = CGSizeMake(searchBar.frame.size.width, searchBar.frame.size.height + 20);
7
  [searchBar setBackgroundImage:[UIImage imageWithColor:AB_White_Color size:size] forBarPosition:UIBarPositionTopAttached barMetrics:UIBarMetricsDefault];
8
9
 searchBar.showsCancelButton = NO;
10
 [searchBar sizeToFit];
11
 searchBar.placeholder = NSLocalizedString(@"public.search", nil);
12
 searchBar.delegate = self;
13
 searchBar.barTintColor = [UIColor whiteColor]; //调整iPhone x上背景颜色的改变

经测试后,将UIBarMetricsDefault修改为UIBarMetricsDefaultPrompt即可

其他机型上还是使用UIBarMetricsDefault

解决黑线问题

1
UIBarMetrics barMetrics = UIBarMetricsDefault;
2
if (IPHONE_X) {
3
    barMetrics = UIBarMetricsDefaultPrompt;
4
}
5
[searchBar setBackgroundImage:[UIImage imageWithColor:AB_White_Color size:size] forBarPosition:UIBarPositionTopAttached barMetrics:barMetrics];

效果图:
image.png

#
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×