如何UITextField
辞职UISearchBar
?我尝试使用以下代码
UIView * subView;
NSArray * subViews = [self.searchBar subviews];
for(subView in subViews)
{
if( [subView isKindOfClass:[UITextField class]] )
{
((UITextField*)subView).delegate=self;
((UITextField*)subView).returnKeyType=UIReturnKeyDone;
break;
}
}
for (UIView *subview in self.searchBar.subviews)
{
if ([subview conformsToProtocol:@protocol(UITextInputTraits)])
{
[(UITextField *)subview setClearButtonMode:UITextFieldViewModeWhileEditing];
}
}
并添加了 TextField Delegate 方法
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
这可行,但如果文本字段不为空,这只会让键盘退出。如果文本字段为空,则它不会辞职