0

我似乎无法从我的 SearchBar 中显示我的自定义键盘。它适用于 UITextField 和 UITextView。我从类似问题的网络中收集了一些此代码,但仍然无法使其正常工作。这是所有代码:

@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