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

那么,在发布这个问题之前,我已经经历了一些体面的护目镜,但没有找到正确的答案。 我无法真正解释我的整个应用场景,因为解释起来有点复杂。 所以,让我提出这个问题非常简单。 我如何更改UIKeyBoard的框架.ie我想要UIKeyBoard旋转或向上翻译90度以支持我的视图位置。 我有没有办法?


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

虽然创建自定义键盘需要一点时间,但它适用于较旧的iOS版本(UITextField上的inputView可在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

执行:

#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

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

- (void)viewDidLoad
{
    [super viewDidLoad];

    CustomKeyboardView *customKeyboard = [[CustomKeyboardView alloc] initForUnderlayingView:self.view];
    textField.inputView = customKeyboard;
}
链接地址: http://www.djcxy.com/p/59179.html

上一篇: How to change the frame of UIKeyBoard programmatically in iOS

下一篇: Losing Gesture Recognizers in UIPopoverController