0

検索バーからカスタム キーボードを表示できないようです。UITextField と UITextView で動作します。このコードの一部を同様の質問の Web から収集しましたが、まだ機能しません。すべてのコードは次のとおりです。

@interface CustomSearchBar : UISearchBar
@property (readwrite, retain) UIView *inputView;
@end

//////////////
@implementation CustomSearchBar
@synthesize inputView;

//////////カスタム キーボード //.h ファイル//////

#import <UIKit/UIKit.h>
#import "CustomSearchBar.h"

@interface PKCustomKeyBoard : UIView <UIInputViewAudioFeedback, UISearchBarDelegate>

 @property (strong, nonatomic) IBOutlet UIImageView *keyboardBackground;
 @property (strong, nonatomic) IBOutletCollection(UIButton) NSArray *characterKeys;
 @property (strong, nonatomic) IBOutlet UIButton *deleteButton;

 @property (strong) id<UITextInput> textView;
 @property (strong, nonatomic) CustomSearchBar *customSearchBar;

 - (IBAction)deletePressed:(id)sender;
 - (IBAction)characterPressed:(id)sender;

 @end

//////////カスタム キーボード //.m ファイル//////

- (id)init 
{

UIInterfaceOrientation orientation = [[UIDevice currentDevice] orientation];
CGRect frame;

if(UIDeviceOrientationIsLandscape(orientation))
    frame = CGRectMake(0, 0, 1024, 352);
else
    frame = CGRectMake(0, 0, 768, 264);

self = [super initWithFrame:frame];

if (self) {
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"PKCustomKeyBoard" owner:self options:nil];
    [[nib objectAtIndex:0] setFrame:frame];
    self = [nib objectAtIndex:0];
}


NSMutableArray *buttons = [NSMutableArray arrayWithArray:self.characterKeys];

for (UIButton *b in buttons) {
    [b setBackgroundImage:[PKCustomKeyBoard imageFromColor:[UIColor colorWithWhite:0.5 alpha:0.5]]
                 forState:UIControlStateHighlighted];
    b.layer.cornerRadius = 7.0;
    b.layer.masksToBounds = YES;
    b.layer.borderWidth = 0;
}

[self loadCharactersWithArray:kChar];


_customSearchBar.delegate = self;


return self;
  }


-(void) setCustomSearchBar:(CustomSearchBar *)customSearchBar
 {
if ([customSearchBar isKindOfClass:[CustomSearchBar class]])
   [customSearchBar setInputView:self];

_customSearchBar = customSearchBar;
 }


-(void)setTextView:(id<UITextInput>)textView {

if ([textView isKindOfClass:[UITextView class]])
    [(UITextView *)textView setInputView:self];
else if ([textView isKindOfClass:[UITextField class]])
    [(UITextField *)textView setInputView:self];

_textView = textView;

 }

-(id<UITextInput>)textView {
return _textView;
 }



  -(void)loadCharactersWithArray:(NSArray *)a {
int i = 0;
for (UIButton *b in self.characterKeys) {
    [b setTitle:[a objectAtIndex:i] forState:UIControlStateNormal];
    [b.titleLabel setFont:kFont];
    [b.titleLabel setTextColor:[UIColor blackColor]];
    i++;
}
  }

 - (BOOL) enableInputClicksWhenVisible {
 return YES;
   }


- (IBAction)deletePressed:(id)sender {
[[UIDevice currentDevice] playInputClick];
[self.textView deleteBackward];
[[NSNotificationCenter defaultCenter] postNotificationName:UITextViewTextDidChangeNotification
                                                    object:self.textView];
if ([self.textView isKindOfClass:[UITextView class]])
    [[NSNotificationCenter defaultCenter] postNotificationName:UITextViewTextDidChangeNotification object:self.textView];
else if ([self.textView isKindOfClass:[UITextField class]])
    [[NSNotificationCenter defaultCenter] postNotificationName:UITextFieldTextDidChangeNotification object:self.textView];
  }

 - (IBAction)characterPressed:(id)sender {
[[UIDevice currentDevice] playInputClick];
UIButton *button = (UIButton *)sender;
NSString *character = [NSString stringWithString:button.titleLabel.text];

if ([character  isEqualToString:@"space"])
    character = @" ";

[self.textView insertText:character];


if ([self.textView isKindOfClass:[UITextView class]])
    [[NSNotificationCenter defaultCenter] postNotificationName:UITextViewTextDidChangeNotification object:self.textView];
else if ([self.textView isKindOfClass:[UITextField class]])
    [[NSNotificationCenter defaultCenter]  postNotificationName:UITextFieldTextDidChangeNotification object:self.textView];
     }


+ (UIImage *) imageFromColor:(UIColor *)color {
CGRect rect = CGRectMake(0, 0, 1, 1);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}

@end

//////////////ViewController.hファイル///////////////////////////////

@interface SearchVC : UIViewController
@property (weak, nonatomic) IBOutlet CustomSearchBar *customSearchBar;
@property (weak, nonatomic) IBOutlet UITextField *myTextField;
@property (weak, nonatomic) IBOutlet UITextView *myTextView;
@end

//////////////ViewController.mファイル///////////////////////////////

@interface SearchVC ()

@end

 @implementation SearchVC
@synthesize customSearchBar;
@synthesize myTextView,myTextField;


- (void)viewDidLoad
{
 [super viewDidLoad];

    PKCustomKeyBoard *customKeyboard = [[PKCustomKeyBoard alloc] init];
    [customKeyboard setTextView:myTextView];
  //  [customKeyboard setTextView:myTextField];
    [customKeyboard setCustomSearchBar:self.customSearchBar];
 }
}
4

0 に答える 0