问题
In iOS6 , I use the below code to change inputView of UISearchbar
for (UIView *view in _searchBar.subviews) {
if ([view isKindOfClass: [UITextField class]]) {
UITextField* textField = (UITextField*)view;
[textField setInputView:_myKeyboard];
break;
}
}
UISearchbar in iOS7 had changed, I don't know how to find textField to change inputView. Please help! Thanks!
回答1:
The hierarchy of subview has been changed in iOS7, so you can use the following code:
// subviews
NSArray *searchBarSubViews = [[self.searchBar.subviews objectAtIndex:0] subviews];
for (UIView *view in searchBarSubViews) {
if([view isKindOfClass:[UITextField class]])
{
UITextField* search=(UITextField*)view;
[search setFont:[UIFont fontWithName:@"MyCustomFont" size:15]];
search.delegate = self;
[search setInputView:self.customKeyboard];
[self.customKeyboard setTextView:search];
}
}
[self.searchBar reloadInputViews];
回答2:
Use following code :
NSArray *subViewsOfSearchBar = [[self.YOurSearchBar.subviews objectAtIndex:0] subviews];
for(int i =0; i< subViewsOfSearchBar.count; i++) {
if([[subViewsOfSearchBar objectAtIndex:i] isKindOfClass:[UITextField class]])
{
UITextField *searchTxtField=(UITextField*)[subViewsOfSearchBar objectAtIndex:i];
[searchTxtField setInputView:self.customKeyboard];
}
}
[self.searchBar reloadInputViews];
回答3:
Try this code :
for (UIView *view in _searchBar.inputView.subviews) {
if ([view isKindOfClass: [UITextField class]]) {
UITextField* textField = (UITextField*)view;
[textField setInputView:_myKeyboard];
break;
}
}
in iOS 7 subview is not working. From iOS7 UI objects have a extra wrapper/container. May be this code will help you.
来源:https://stackoverflow.com/questions/20278677/how-to-change-inputview-of-uisearchbar-in-ios7