我正在为我的应用程序定制键盘,它在UITextField上工作得很好。但是UISearchBar不支持inputView
- (UIView *)inputView {
if(self.keyboard==nil)
{
self.keyboard=[[[MMKeyboard alloc] initWithNibName:@"MMKeyboard" bundle:nil]autorelease];
}
NSLog(@"HERE");
return self.keyboard.view;
}如何在UISearchBar中替换为我的自定义UITextField,或者如何在UISearchBar中实现inputView?
发布于 2011-04-25 00:10:20
我像下面这样解决了
#import <Foundation/Foundation.h>
#import "MMKeyboard.h"
@interface MMSBar : UISearchBar {
MMKeyboard* keyboard;
}
@property(nonatomic,retain)MMKeyboard* keyboard;
@end
- (void)layoutSubviews {
if(self.keyboard==nil)
{
self.keyboard=[[[MMKeyboard alloc] initWithNibName:@"MMKeyboard" bundle:nil]autorelease];
}
UITextView *searchField;
NSUInteger numViews = [self.subviews count];
for(int i = 0; i < numViews; i++) {
if([[self.subviews objectAtIndex:i] isKindOfClass:[UITextField class]]) {
searchField = [self.subviews objectAtIndex:i];
[searchField setInputView:keyboard.view];
}
}
[super layoutSubviews];
}发布于 2011-04-24 18:28:44
就像这段代码所做的那样,但是您要更改inputView
// loop around subviews of UISearchBar
for (UIView *searchBarSubview in [searchBar subviews]) {
if ([searchBarSubview conformsToProtocol:@protocol(UITextInputTraits)]) {
@try {
// set style of keyboard
[(UITextField *)searchBarSubview setKeyboardAppearance:UIKeyboardAppearanceAlert];
// always force return key to be enabled
[(UITextField *)searchBarSubview setEnablesReturnKeyAutomatically:NO];
}
@catch (NSException * e) {
// ignore exception
}
}
}有关更多详细信息,请参阅此帖子:
https://stackoverflow.com/questions/5769999
复制相似问题