gpt4 book ai didi

ios - 移动位于键盘下方的内容

转载 作者:行者123 更新时间:2023-12-01 18:58:46 25 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





How can I make a UITextField move up when the keyboard is present - on starting to edit?

(98 个回答)


7年前关闭。




我有一个关于“移动位于键盘下方的内容”的问题。

我有一个简单的“登录” View ,其中包含 2 个 UITextField(用户名和密码)和一个登录按钮。
当用户点击这些文本字段之一时,键盘会出现并遮盖登录按钮和密码文本字段。
所以我实现了苹果和其他人提出的解决方案(见下面的链接):

https://developer.apple.com/library/ios/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html

http://ios-blog.co.uk/tutorials/how-to-make-uitextfield-move-up-when-keyboard-is-present/

但问题是键盘首先移动,只有当键盘完成移动时,2 个 UITextField 和登录按钮才会移动。因此,它不是用户体验的最佳解决方案......
你知道我可以在 移动键盘和 2 个 UITextFields同时 ?就像在 iOS Facebook 登录页面上一样。

谢谢 !

最佳答案

好吧,您可以通过两种方式做到这一点:简单的方式和困难的方式。

简单的方法:代替 ViewController,使用 TableViewController 并创建包含文本字段的自定义单元格。 TableViewController 将负责在键盘启动时为您向上移动 View 。然后,您所需要担心的就是在您希望键盘消失时关闭键盘。

困难的方法:使用 ViewController。
为了让它工作,你需要有 2 个通知,像这样的(把它放在你的 viewDidLoad 方法中):

        // Keyboard notifications so things can move up when the keyboard shows up
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];

然后您需要创建您在 NotificationCenter 中调用的 2 个方法,以实际执行屏幕的移动:
- (void) keyboardWillShow: (NSNotification*) aNotification
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
CGRect rect = [[self view] bounds];
rect.origin.y -= 150;
[[self view] setFrame: rect];
[UIView commitAnimations];
}

// Call this to make the keyboard move everything down
- (void) keyboardWillHide: (NSNotification*) aNotification
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
CGRect rect = [[self view] frame];
if (rect.origin.y == -150)
{
rect.origin.y += 150;
}
[[self view] setFrame: rect];
[UIView commitAnimations];
}

在此代码中,值 150 是键盘将向上/向下移动的任意点数。无论屏幕大小如何,您都可以更改它或进行一些花哨的计算以获得屏幕的百分比。

希望能帮助到你!

关于ios - 移动位于键盘下方的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24412187/

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