我制作了一个自定义类别来公开 returnKeyType。只需导入并执行self.addField.returnKeyType = UIReturnKeySearch;
或您喜欢设置为 returnKeyType 的任何内容。您也可以在键盘已经打开时动态使用它。它会自动刷新。
这里是:
界面:
/*
Simple hack for exposing returnKeyType for UISearchBar without private APIs
For you from CodeBuffet ;)
Enjoy!
~PW
*/
#import <UIKit/UIKit.h>
@interface UISearchBar (ReturnType)
@property (nonatomic, readwrite) UIReturnKeyType returnKeyType;
@end
执行:
#import "UISearchBar+ReturnType.h"
@implementation UISearchBar (ReturnType)
UITextField *textField;
- (UITextField *) traverseForTextViewInViews: (NSArray*) views
{
// Traverse over all views recursively until we find the tresure TextField hidden deep the UISearchBar ocean!
for(UIView *subView in views) {
if([subView conformsToProtocol:@protocol(UITextInputTraits)]) {
return (UITextField *) subView;
}
UITextField *tv = [self traverseForTextViewInViews:subView.subviews];
if (tv) {
return tv;
}
}
return nil;
}
#pragma mark Custom Setters & Getters
- (void)setReturnKeyType:(UIReturnKeyType)returnKeyType
{
if (!textField) {
textField = [self traverseForTextViewInViews:self.subviews];
}
textField.returnKeyType = returnKeyType;
[self reloadInputViews];
[textField reloadInputViews];
}
- (UIReturnKeyType)returnKeyType
{
if (!textField) {
textField = [self traverseForTextViewInViews:self.subviews];
}
return textField.returnKeyType;
}
@end