4

从 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

我的编辑器.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

我的键盘.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

测试步骤如下:

  1. 启动应用程序。
  2. 点击编辑器,键盘将显示。
  3. 确认您不能通过拖动键盘的右下角来移动键盘。
  4. 再次点击编辑器,键盘将隐藏,并显示警报视图。
  5. 关闭警报视图。
  6. 第三次点击编辑器,键盘将显示。
  7. 验证现在您可以通过拖动键盘的右下角来移动键盘。

完毕。

我想知道如何在启动后一开始就使键盘可以脱离。任何信息表示赞赏!

4

0 回答 0