3

我正在为我的应用程序制作自定义键盘,它与 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 ?

4

2 回答 2

2

正如此代码所做的那样,但您改为更改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
  }
 }
}

有关更多详细信息,请参阅此帖子:

iphone UISearchBar 完成按钮始终启用

于 2011-04-24T10:28:43.783 回答
2

我解决了如下

#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-24T16:10:20.393 回答