UISearchBar with InputView

拜拜、爱过 提交于 2019-12-06 06:58:05

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!