如何设置UIKeyboard的位置

发布于 2024-09-24 23:27:45 字数 49 浏览 0 评论 0原文

我有一个位于视图底部的搜索栏。

如何设置UIKeyboard的位置?

I have a search bar which is at the bottom of the view.

How can I set the position of the UIKeyboard?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

寂寞笑我太脆弱 2024-10-01 23:27:45

使用 CGAffineTransform
至少从我观察和测试来看,4.0之前的操作系统版本需要定义转换,
从操作系统 4.0 和更高版本开始,操作系统负责键盘定位。
这就是为什么我在设置 Transform 之前检查 systemVersion。

    if ([[[UIDevice currentDevice] systemVersion] floatValue] < 4.0) {
        CGAffineTransform translate = CGAffineTransformMakeTranslation(xx.0, yy.0);//use x,y values
        [self setTransform:translate];
    }

use CGAffineTransform
At least from what I have observed and tested that OS version before 4.0 needs to define the transform,
from the os 4.0 and greater OS is taking care of keyboard positioning.
that's why here I am checking for the systemVersion before setting Transform.

    if ([[[UIDevice currentDevice] systemVersion] floatValue] < 4.0) {
        CGAffineTransform translate = CGAffineTransformMakeTranslation(xx.0, yy.0);//use x,y values
        [self setTransform:translate];
    }
余厌 2024-10-01 23:27:45

您没有设置键盘的位置。系统会自动为您完成。

您需要做的是使用键盘通知将搜索栏移动到可见部分

You don't set the position of the keyboard. The system will do it for you automatically.

What you need to do is to move your searchbar to a visible part using keyboard notifications

无边思念无边月 2024-10-01 23:27:45

您无法设置键盘的位置,只能询问键盘在哪里并适当地组织其他视图。

// Somewhere you need to register for keyboard notifications

- (void)viewDidLoad {
    [super viewDidLoad];

    // Register for keyboard notifications
    NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
    [defaultCenter addObserver:self selector:@selector(keyboardWasShown:)
                          name:UIKeyboardDidShowNotification object:nil];
    //... do something
}

// At some point you need to unregister for notifications
- (void)viewWillHide {
    NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
    [defaultCenter removeObserver:self];
}

- (void)keyboardWasShown:(NSNotification*)aNotification
{
    // Caution, this notification can be sent even when the keyboard is already visible
    // You'll want to check for and handle that situation
    NSDictionary* info = [aNotification userInfo];

    NSValue* aValue = [info objectForKey:UIKeyboardFrameEndUserInfoKey];

    CGSize keyboardSize = [aValue CGRectValue].size;

    //... do something
}

You cannot set the position of the keyboard, you can only ask the keyboard where it is and organize your other views appropriately.

// Somewhere you need to register for keyboard notifications

- (void)viewDidLoad {
    [super viewDidLoad];

    // Register for keyboard notifications
    NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
    [defaultCenter addObserver:self selector:@selector(keyboardWasShown:)
                          name:UIKeyboardDidShowNotification object:nil];
    //... do something
}

// At some point you need to unregister for notifications
- (void)viewWillHide {
    NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
    [defaultCenter removeObserver:self];
}

- (void)keyboardWasShown:(NSNotification*)aNotification
{
    // Caution, this notification can be sent even when the keyboard is already visible
    // You'll want to check for and handle that situation
    NSDictionary* info = [aNotification userInfo];

    NSValue* aValue = [info objectForKey:UIKeyboardFrameEndUserInfoKey];

    CGSize keyboardSize = [aValue CGRectValue].size;

    //... do something
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文