在 iOS 中向左滑动键盘

Sliding the keyboard to the left in iOS

我想知道是否有办法在用户按下按钮时将第一响应键盘向左或向右滑动。

http://cl.ly/image/143K3t403d1m/1.png

我将按钮设为键盘附件视图。当它被点击时,键盘应该向左滑动,显示另一个自定义输入面板。

有什么想法吗?

如果你想要一个可以从系统中选择的键盘(使用地球键),你需要阅读this article about creating a Custom Keyboard App Extension for iOS 8

如果您只想能够在 inputView 个对象之间切换,可以使用以下代码:

//  ViewController.m
#import "ViewController.h"
@interface ViewController () {

}
#pragma mark -
#pragma mark - UI Controls
@property (strong, nonatomic) UIInputView *inputView;
@property (strong, nonatomic) UITextField *textField;
@end
@implementation ViewController
#pragma mark -
#pragma mark - View Lifecycle
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUserInterface];
}
#pragma mark -
#pragma mark - Keyboard switching
- (void)switchKeyboard {
    // Simply toggle the "inputView" for self.textField
    if (self.textField.inputView == nil) {
        self.textField.inputView    = self.inputView;
    } else {
        self.textField.inputView    = nil;
    }
    [self.textField resignFirstResponder];
    [self.textField becomeFirstResponder];
}
#pragma mark -
#pragma mark - UI Setup 
// All of the code below here
// is for pure, in-code AutoLayout
- (void)setupUserInterface {
    [self createControls];
    [self setupControls];
    [self layoutControls];
}
- (void)createControls {
    self.textField                      = [[UITextField alloc] init];
    self.textField.borderStyle          = UITextBorderStyleRoundedRect;
    self.inputView                      = [[UIInputView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 216.0f)];
    self.inputView.backgroundColor      = [UIColor lightGrayColor];
}
- (void)setupControls {
    UIToolbar *toolbar                  = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 44.0f)];
    toolbar.backgroundColor             = [UIColor lightGrayColor];
    // When the button is tapped, it'll execute "switchKeyboard" above
    UIBarButtonItem *switchButton       = [[UIBarButtonItem alloc] initWithTitle:@"Switch" style:UIBarButtonItemStyleDone target:self action:@selector(switchKeyboard)];
    toolbar.items                       = @[switchButton];
    self.textField.inputAccessoryView   = toolbar;
    [self.textField setTranslatesAutoresizingMaskIntoConstraints:NO];

}
- (void)layoutControls {
    [self.view addSubview:self.textField];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[textfield]-|"
                                                                      options:0
                                                                      metrics:nil
                                                                        views:@{@"textfield": self.textField}]];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(height)-[textfield(==height)]"
                                                                      options:0
                                                                      metrics:@{@"height"   : @(40)}
                                                                        views:@{@"textfield": self.textField}]];
}
@end

Caveat: You will need to do a lot of tweaking to make sure the user experience is good. In my testing, on the simulator (iPhone 4S), this works ok, but isn't the smoothest user experience.