How to make custom keyboard undock-able?

302 views Asked by At

As of iOS 5, iPad keyboard can undock/split.

But my custom keyboard can not undock when my app is first launched. Only after, say, an alert view is shown and dismissed, the keyboard becomes undock-able.

I made a test project based on the Single View Application template as follows:

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

The test steps are as follows:

  1. Launch the app.
  2. Tap the editor, and the keyboard will show.
  3. Verify that you can not move the keyboard by dragging the lower-right corner of it.
  4. Tap the editor again, the keyboard will hide, and an alert view will show.
  5. Dismiss the alert view.
  6. Tap the editor a third time, and the keyboard will show.
  7. Verify that now you can move the keyboard by dragging the lower-right corner of it.

Done.

I wonder how to make the keyboard undock-able at the very beginning after launch. Any info is appreciated!

0

There are 0 answers