从iOS 5开始,iPad键盘可以移除/拆分。
但是我的自定义键盘在我的应用首次启动时无法取消停靠。只有在显示和解除警报视图后,键盘才能解除连接。
我基于Single View Application模板制作了一个测试项目,如下所示:
MyViewController.m :
#import "MyViewController.h"
#import "MyEditor.h"
@implementation MyViewController
- (void)viewDidLoad {
[super viewDidLoad];
MyEditor *editor = [[MyEditor alloc] initWithFrame:CGRectMake(0, 640, 768, 100)];
editor.backgroundColor = [UIColor cyanColor];
[self.view addSubview:editor];
}
@end
MyEditor.m :
#import "MyEditor.h"
#import "MyKeyboard.h"
@implementation MyEditor
- (CGSize)intrinsicContentSize {
return CGSizeMake(UIViewNoIntrinsicMetric, 100);
}
- (UIView *)inputView {
return [MyKeyboard sharedKeyboard];
}
- (BOOL)canBecomeFirstResponder {
return YES;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
if ([self isFirstResponder]) {
[self resignFirstResponder];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:nil delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
[alertView show];
}
else {
[self becomeFirstResponder];
}
}
@end
MyKeyboard.m :
#import "MyKeyboard.h"
@implementation MyKeyboard
+ (MyKeyboard *)sharedKeyboard {
static MyKeyboard *sharedKeyboard;
if (sharedKeyboard == nil) {
sharedKeyboard = [[MyKeyboard alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
}
return sharedKeyboard;
}
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[super setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
[super setBackgroundColor:[UIColor yellowColor]];
}
return self;
}
@end
测试步骤如下:
完成。
我想知道如何在发布后的一开始就让键盘脱离接触。任何信息表示赞赏!