问题
I'm making custom keyboard for my app and it's working fine with UITextField. But UISearchBar don't support inputView
- (UIView *)inputView {
if(self.keyboard==nil)
{
self.keyboard=[[[MMKeyboard alloc] initWithNibName:@"MMKeyboard" bundle:nil]autorelease];
}
NSLog(@"HERE");
return self.keyboard.view;
}
How to replace with my custom UITextField in UISearchBar or how to implement inputView in UISearchBar ?
回答1:
As this code is doing but you instead change the 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
}
}
}
See this post for more details:
iphone UISearchBar Done button always enabled
回答2:
I solved like following
#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];
}
来源:https://stackoverflow.com/questions/5769999/uisearchbar-with-inputview