gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 03:32:32 24 4
gpt4 key购买 nike

好吧,在发布这个问题之前,我已经进行了一些体面的目击,但未能找到正确的答案。我真的不能在这里解释我的整个应用场景,因为解释起来有点复杂。所以,让我让这个问题非常非常简单。我如何更改 UIKeyBoard 的框架。即我希望 UIKeyBoard 旋转或向上平移 90 度 以支持我的 View 位置。我有出路吗?

最佳答案

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

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

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

接口(interface):

#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;
}

关于iphone - 如何在 iOS 中以编程方式更改 UIKeyBoard 的框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10444349/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com