在iOS 11上,搜索文本字段在搜索栏中的位置错误 [英] Wrong placement of search text field inside search bar on iOS 11
问题描述
我使用iOS 11中引入的新API将UISearchController用作导航栏的一部分,并在ViewController的viewDidLoad中以以下方式使用它
-(void)viewDidLoad {[super viewDidLoad];[_table setDataSource:self];[_table setDelegate:self];searchController = [[UISearchController alloc] initWithSearchResultsController:nil];[self.navigationItem setSearchController:searchController];[self.navigationItem setHidesSearchBarWhenScrolling:NO];[searchController.searchBar setBackgroundColor:[UIColor greenColor]];}
但是,搜索文本字段显示在搜索栏内的错误位置.请看下面的屏幕截图.
之后:
在(void)viewDidLoad中的Obj-C:使用此代码:
如果(@available(iOS 11,*)){UITextField * textField = [self.searchController.searchBar valueForKey:@"searchField"];UIView * backgroundView = textField.subviews.firstObject;backgroundView.backgroundColor = UIColor.whiteColor;backgroundView.layer.cornerRadius = 10;backgroundView.clipsToBounds = YES;}
在viewDidLoad()中
Swift:使用此代码:
如果#available(iOS 11.0,*){如果让textfield = scb.value(forKey:"searchField")为?UITextField {textfield.textColor = UIColor.blue如果让backgroundview = textfield.subviews.first {backgroundview.backgroundColor = UIColor.whitebackgroundview.layer.cornerRadius = 10;backgroundview.clipsToBounds = true;}}}
I am using UISearchController as part of navigation bar using the new APIs introduced in iOS 11. I am using it in the following manner in my ViewController's viewDidLoad
- (void)viewDidLoad {
[super viewDidLoad];
[_table setDataSource:self];
[_table setDelegate:self];
searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
[self.navigationItem setSearchController:searchController];
[self.navigationItem setHidesSearchBarWhenScrolling:NO];
[searchController.searchBar setBackgroundColor:[UIColor greenColor]];
}
However, the search text field is rendered at the wrong position inside the search bar. Look at the following screenshot.
I inspected the view hierarchy and found that UISearchBarTextField object (which is not accessible directly to devs) in the search bar has a frame.y value of 1 which is probably causing this issue. I have tested this on iOS 11 beta 10 and iOS 11 GM.
Is this a known issue? Is there any fix for this? Or is it something I am doing wrong on my end?
Any help will be appreciated (:
Here's code which shows how to change the background colour of textField in searchBar on iOS 11.
Before:
After:
Obj-C: in (void)viewDidLoad use this code:
if (@available(iOS 11, *)) {
UITextField *textField = [self.searchController.searchBar valueForKey:@"searchField"];
UIView *backgroundView = textField.subviews.firstObject;
backgroundView.backgroundColor = UIColor.whiteColor;
backgroundView.layer.cornerRadius = 10;
backgroundView.clipsToBounds = YES;
}
Swift: in viewDidLoad() use this code:
if #available(iOS 11.0, *) {
if let textfield = scb.value(forKey: "searchField") as? UITextField {
textfield.textColor = UIColor.blue
if let backgroundview = textfield.subviews.first {
backgroundview.backgroundColor = UIColor.white
backgroundview.layer.cornerRadius = 10;
backgroundview.clipsToBounds = true;
}
}
}
这篇关于在iOS 11上,搜索文本字段在搜索栏中的位置错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!