How to change the frame of UIKeyBoard programmatically in iOS

Well, i have gone through some decent goggling before posting this question but was unsuccessful in finding the right answers. I cant really explain my entire app scenario here as it is a bit complex to explain. So, let me make this question very very simple. How can i change the frame of the UIKeyBoard .ie I want the UIKeyBoard to rotate or translate 90 degrees upwards to support my view position. Is there a way out for me?


You can't change the default keyboard. You can, however, create a custom UIView to be used as keyboard replacement by setting it as inputView on, for example, a UITextField.

While creating a custom keyboard takes a bit of time, it works well with older iOS versions ( inputView on the UITextField is available in iOS 3.2 and later) and supports physical keyboards (the keyboard is hidden automatically if one is connected).

Here's some sample code to create a vertical keyboard:

Interface:

#import <UIKit/UIKit.h>

@interface CustomKeyboardView : UIView

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

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

@end

Implementation:

#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

Using the custom keyboard in some UIViewController:

- (void)viewDidLoad
{
    [super viewDidLoad];

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

上一篇: COUNT()函数每次存储该值并将其加1?

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