Ios 如何使自定义键盘可以解锁?

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];

从iOS 5开始,iPad键盘可以解锁/拆分

但当我的应用程序首次启动时,我的自定义键盘无法解锁。只有在显示并取消警报视图后,键盘才能解锁

我做了一个基于单视图应用程序模板的测试项目,如下所示:

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
#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
测试步骤如下:

  • 启动应用程序
  • 轻触编辑器,键盘将显示
  • 通过拖动键盘的右下角,确认无法移动键盘
  • 再次点击编辑器,键盘将隐藏,并显示警报视图
  • 关闭警报视图
  • 第三次轻触编辑器,键盘将显示
  • 确认现在可以通过拖动键盘右下角来移动键盘
  • 完成了

    我想知道如何使键盘在启动后一开始就可以松开。欢迎提供任何信息