如何在iOS中以编程方式更改UIKeyBoard的框架

A f*_*pha 7 iphone uikeyboard ipad ios

好吧,在发布这个问题之前,我已经经历了一些不错的调查,但找不到合适的答案却没有成功.我无法在这里解释我的整个应用场景,因为解释起来有点复杂.所以,让我非常简单地提出这个问题.如何更改UIKeyBoard.ie 的框架我希望UIKeyBoard向上旋转或平移90度以支持我的视图位置.我有出路吗?

And*_*Ley 4

您无法更改默认键盘。inputView但是,您可以通过将其设置为on(例如 UITextField)来创建要用作键盘替换的自定义 UIView 。

虽然创建自定义键盘需要一些时间,但它适用于较旧的 iOS 版本(inputViewUITextField 在 iOS 3.2 及更高版本中可用)并支持物理键盘(如果连接,键盘会自动隐藏)。

以下是创建垂直键盘的一些示例代码:

界面:

#import <UIKit/UIKit.h>

@interface CustomKeyboardView : UIView

@property (nonatomic, strong) UIView *innerInputView;
@property (nonatomic, strong) UIView *underlayingView;

- (id)initForUnderlayingView:(UIView*)underlayingView;

@end
Run Code Online (Sandbox Code Playgroud)

执行:

#import "CustomKeyboardView.h"

@implementation CustomKeyboardView

@synthesize innerInputView=_innerInputView;
@synthesize underlayingView=_underlayingView;

- (id)initForUnderlayingView:(UIView*)underlayingView
{
    //  Init a CustomKeyboardView with the size of the underlying view
    //  You might want to set an autoresizingMask on the innerInputView.
    self = [super initWithFrame:underlayingView.bounds];
    if (self) 
    {
        self.underlayingView = underlayingView;

        //  Create the UIView that will contain the actual keyboard
        self.innerInputView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, underlayingView.bounds.size.height)];

        //  You would need to add your custom buttons to this view; for this example, it's just red
        self.innerInputView.backgroundColor = [UIColor redColor];

        [self addSubview:self.innerInputView];
    }
    return self;
}

-(id)hitTest:(CGPoint)point withEvent:(UIEvent *)event 
{
    //  A hitTest is executed whenever the user touches this UIView or any of its subviews.

    id hitTest = [super hitTest:point withEvent:event];

    //  Since we want to ignore any clicks on the "transparent" part (this view), we execute another hitTest on the underlying view.
    if (hitTest == self)
    {
        return [self.underlayingView hitTest:point withEvent:nil];
    }

    return hitTest;
}

@end
Run Code Online (Sandbox Code Playgroud)

在一些 UIViewController 中使用自定义键盘:

- (void)viewDidLoad
{
    [super viewDidLoad];

    CustomKeyboardView *customKeyboard = [[CustomKeyboardView alloc] initForUnderlayingView:self.view];
    textField.inputView = customKeyboard;
}
Run Code Online (Sandbox Code Playgroud)