[英]iPhone keyboard sizes
Is there a way to get the keyboard size programmatically before the keyboard is presented? 有没有办法在呈现键盘之前以编程方式获取键盘的大小? In Objective-C
在Objective-C中
I need to set view.height constraint to be the same as keyboard.height programmatically. 我需要以编程方式将view.height约束设置为与keyboard.height相同。 And it needs to happen before the keyboard is presented, so the view don't get this ugly constrain animation after the ViewController is presented.
并且它需要在呈现键盘之前发生,因此在呈现ViewController之后,视图不会得到这种难看的约束动画。
I assume you present the keyboard by calling becomeFirstResponder
on some UI component. 我假设您通过在某些UI组件上调用
becomeFirstResponder
来显示键盘。
If the keyboard appears after your view is presented, you should check where that call is performed. 如果在显示视图后出现键盘,则应检查在何处执行该调用。 Calling it in
viewDidLoad
or similarly early should cause the keyboard to be shown as the view animates in. 在
viewDidLoad
或类似的早期调用它会导致在显示动画时显示键盘。
Your layout should also handle the keyboard changes properly. 您的布局还应该正确处理键盘更改。 The keyboard size can change even after it's presented.
键盘大小即使显示后也可以更改。 For example the emoji/quick type keyboards are taller than the default keyboard.
例如,表情符号/快速型键盘比默认键盘高。
You should perform your constraint changes in a combination of UIKeyboard[Will/Did]ShowNotification
, UIKeyboard[Will/Did]HideNotification
and UIKeyboardWillChangeFrameNotification
. 您应该结合使用
UIKeyboard[Will/Did]ShowNotification
, UIKeyboard[Will/Did]HideNotification
和UIKeyboardWillChangeFrameNotification
来进行约束更改。 In your case, UIKeyboardWillShowNotification
should do the trick. 在您的情况下,
UIKeyboardWillShowNotification
应该可以解决问题。
The userInfo
dictionary contains a lot of information about the keyboard. userInfo
词典包含许多有关键盘的信息 。 You find the final frame of the keyboard in UIKeyboardFrameEndUserInfoKey
. 您可以在
UIKeyboardFrameEndUserInfoKey
找到键盘的最后一帧。 If you animate the changes in your layout, you can use values in UIKeyboardAnimationCurveUserInfoKey
and UIKeyboardAnimationDurationUserInfoKey
to animate with the same animation as the keyboard. 如果要对布局中的更改进行动画处理,则可以使用
UIKeyboardAnimationCurveUserInfoKey
和UIKeyboardAnimationDurationUserInfoKey
值来为与键盘相同的动画进行动画处理。
- (void)viewDidLoad {
[super viewDidLoad];
// Don't forget to remove the observer when appropriate.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
[self.textField becomeFirstResponder];
}
- (void)keyboardWillShow:(NSNotification *)notification {
CGFloat keyboardHeight = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height;
[self.viewHeightConstraint setConstant:keyboardHeight];
// You can also animate the constraint change.
}
Such setup will also work if the keyboard is presented from the get-go. 如果从一开始就出现了键盘,这种设置也将起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.