Ios 如何使自定义键盘可以解锁?
从iOS 5开始,iPad键盘可以解锁/拆分 但当我的应用程序首次启动时,我的自定义键盘无法解锁。只有在显示并取消警报视图后,键盘才能解锁 我做了一个基于单视图应用程序模板的测试项目,如下所示: MyViewController.m:Ios 如何使自定义键盘可以解锁?,ios,ipad,keyboard,dock,inputview,Ios,Ipad,Keyboard,Dock,Inputview,从iOS 5开始,iPad键盘可以解锁/拆分 但当我的应用程序首次启动时,我的自定义键盘无法解锁。只有在显示并取消警报视图后,键盘才能解锁 我做了一个基于单视图应用程序模板的测试项目,如下所示: MyViewController.m: #import "MyViewController.h" #import "MyEditor.h" @implementation MyViewController - (void)viewDidLoad { [super viewDidLoad];
#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
#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
#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
MyEditor.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
#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
#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
我的键盘.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
#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
#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
测试步骤如下: