如何获得iOS 8.3表情符号键盘高度?

时间:2021-02-05 00:13:33

I can handle two events: when keyboard shows and when keyboard hides. Everything worked fine in iOS 8.2 and older.

我可以处理两个事件:键盘显示和键盘隐藏时。在iOS 8.2及更早版本中,一切都运行良好。

But how to handle event when you change your keyboard language? When you change the english keyboard to the emoji keyboard, the height of emoji keyboard (in ios 8.3) is bigger and it hides the content.

但是如何在更改键盘语言时处理事件?当您将英文键盘更改为表情符号键盘时,表情符号键盘的高度(在ios 8.3中)更大并且隐藏了内容。

Or maybe you have a solution how to control iOS 8.3 emoji keyboard height? 如何获得iOS 8.3表情符号键盘高度?

或者你有一个解决方案如何控制iOS 8.3表情符号键盘高度?

3 个解决方案

#1


12  

OK. So looking at my old code, I remembered, I do not use 2 observers (UIKeyboardDidShowNotification/UIKeyboardDidHideNotification). I use a single observer (UIKeyboardWillChangeFrameNotification), that is fired of each event: Keyboard hiding, keyboard showing, keyboard changing frame.

好。所以看着我的旧代码,我记得,我不使用2个观察者(UIKeyboardDidShowNotification / UIKeyboardDidHideNotification)。我使用单个观察者(UIKeyboardWillChangeFrameNotification),即每个事件被触发:键盘隐藏,键盘显示,键盘更改框架。

In my case, the text box and send button are in nested in a UIView and this is view is added in the view of the UIViewController, above everything else.

在我的例子中,文本框和发送按钮嵌套在UIView中,这是视图添加到UIViewController的视图中,高于其他所有内容。

I add the observer in viewDidAppear and remove the observer in viewWillDisappear.(to avoid any notification firing when view is not active).

我在viewDidAppear中添加观察者并删除viewWillDisappear中的观察者。(以避免在视图不活动时触发任何通知)。

The above information is not necessary for your case, just added it for information sake. Relevant code is as follows:

以上信息对您的情况不是必需的,只是为了提供信息而添加它。相关代码如下:

ADD OBSERVER:

添加观察员:

- (void) viewDidAppear:(BOOL)animated {

    [super viewDidAppear:animated];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
}

HANDLE NOTIFICATION:

手柄通知:

- (void) keyboardWillChangeFrame:(NSNotification*)notification {

    NSDictionary* notificationInfo = [notification userInfo];

    CGRect keyboardFrame = [[notificationInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];

    [UIView animateWithDuration:[[notificationInfo valueForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue]
                          delay:0
                        options:[[notificationInfo valueForKey:UIKeyboardAnimationCurveUserInfoKey] integerValue]
                     animations:^{

                         CGRect frame = self.textViewContainer.frame;
                         frame.origin.y = keyboardFrame.origin.y - frame.size.height;
                         self.textViewContainer.frame = frame;

                     } completion:nil];
}

You might have to make a few adjustments to the frame.origin.y... line for correct calculations. I don't know whether you have a UITabBarController or any bars at the bottom. The safest bet here would be:

您可能需要对frame.origin.y ...行进行一些调整才能进行正确的计算。我不知道你是否有一个UITabBarController或底部的任何栏。这里最安全的赌注是:

frame.origin.y = self.view.frame.size.height - keyboardFrame.size.height - X;

Where X is 0 if your VC covers the whole screen. If not, use the heights of any bottom bars.

如果您的VC覆盖整个屏幕,则X为0。如果没有,请使用任何底栏的高度。

#2


6  

I had the same problem. Just replace UIKeyboardFrameBeginUserInfoKey with UIKeyboardFrameEndUserInfoKey . :-)

我有同样的问题。只需用UIKeyboardFrameEndUserInfoKey替换UIKeyboardFrameBeginUserInfoKey即可。 :-)

It worked for me.

它对我有用。

#3


4  

It's worth noting that the emoji keyboard is the same hight as the standard keyboard with suggested text enabled.

值得注意的是,表情符号键盘与启用建议文本的标准键盘相同。

To properly determine keyboard height and adjust your view, add these observers:

要正确确定键盘高度并调整视图,请添加以下观察者:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil];

Then I use the following methods to animate the keyboard adjustment. Really all you need is the keyboardBounds object, but if you happen to be using AutoLayout, this is how you'd do it:

然后我使用以下方法为键盘调整设置动画。你真正需要的只是keyboardBounds对象,但是如果你正好使用AutoLayout,那么你就是这样做的:

- (void)keyboardDidShow:(NSNotification *)notification
{
    [self scrollControlBarTo:notification up:YES];
}

-(void)keyboardDidHide:(NSNotification *)notification
{
    [self scrollControlBarTo:notification up:NO];
}

- (void)scrollControlBarTo:(NSNotification *)notification up:(BOOL)up
{
    [_keyboardControlsBar layoutIfNeeded];
    CGRect keyboardBounds;
    NSDictionary *info = [notification userInfo];
    NSNumber *number = [info objectForKey:UIKeyboardAnimationDurationUserInfoKey];
    double duration = [number doubleValue];
    [[info objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardBounds];
    UIViewAnimationCurve curve = [[info objectForKey:UIKeyboardAnimationCurveUserInfoKey] integerValue];

    [UIView animateWithDuration:duration
                          delay:0
                        options:UIViewAnimationOptionBeginFromCurrentState
                     animations:^{
                         [UIView setAnimationCurve:curve];
                         _keyboardControlsBarBottomConstraint.constant = (up) ? keyboardBounds.size.height : 0;
                         [self.view layoutIfNeeded];
                     } completion:nil];
}

#1


12  

OK. So looking at my old code, I remembered, I do not use 2 observers (UIKeyboardDidShowNotification/UIKeyboardDidHideNotification). I use a single observer (UIKeyboardWillChangeFrameNotification), that is fired of each event: Keyboard hiding, keyboard showing, keyboard changing frame.

好。所以看着我的旧代码,我记得,我不使用2个观察者(UIKeyboardDidShowNotification / UIKeyboardDidHideNotification)。我使用单个观察者(UIKeyboardWillChangeFrameNotification),即每个事件被触发:键盘隐藏,键盘显示,键盘更改框架。

In my case, the text box and send button are in nested in a UIView and this is view is added in the view of the UIViewController, above everything else.

在我的例子中,文本框和发送按钮嵌套在UIView中,这是视图添加到UIViewController的视图中,高于其他所有内容。

I add the observer in viewDidAppear and remove the observer in viewWillDisappear.(to avoid any notification firing when view is not active).

我在viewDidAppear中添加观察者并删除viewWillDisappear中的观察者。(以避免在视图不活动时触发任何通知)。

The above information is not necessary for your case, just added it for information sake. Relevant code is as follows:

以上信息对您的情况不是必需的,只是为了提供信息而添加它。相关代码如下:

ADD OBSERVER:

添加观察员:

- (void) viewDidAppear:(BOOL)animated {

    [super viewDidAppear:animated];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
}

HANDLE NOTIFICATION:

手柄通知:

- (void) keyboardWillChangeFrame:(NSNotification*)notification {

    NSDictionary* notificationInfo = [notification userInfo];

    CGRect keyboardFrame = [[notificationInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];

    [UIView animateWithDuration:[[notificationInfo valueForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue]
                          delay:0
                        options:[[notificationInfo valueForKey:UIKeyboardAnimationCurveUserInfoKey] integerValue]
                     animations:^{

                         CGRect frame = self.textViewContainer.frame;
                         frame.origin.y = keyboardFrame.origin.y - frame.size.height;
                         self.textViewContainer.frame = frame;

                     } completion:nil];
}

You might have to make a few adjustments to the frame.origin.y... line for correct calculations. I don't know whether you have a UITabBarController or any bars at the bottom. The safest bet here would be:

您可能需要对frame.origin.y ...行进行一些调整才能进行正确的计算。我不知道你是否有一个UITabBarController或底部的任何栏。这里最安全的赌注是:

frame.origin.y = self.view.frame.size.height - keyboardFrame.size.height - X;

Where X is 0 if your VC covers the whole screen. If not, use the heights of any bottom bars.

如果您的VC覆盖整个屏幕,则X为0。如果没有,请使用任何底栏的高度。

#2


6  

I had the same problem. Just replace UIKeyboardFrameBeginUserInfoKey with UIKeyboardFrameEndUserInfoKey . :-)

我有同样的问题。只需用UIKeyboardFrameEndUserInfoKey替换UIKeyboardFrameBeginUserInfoKey即可。 :-)

It worked for me.

它对我有用。

#3


4  

It's worth noting that the emoji keyboard is the same hight as the standard keyboard with suggested text enabled.

值得注意的是,表情符号键盘与启用建议文本的标准键盘相同。

To properly determine keyboard height and adjust your view, add these observers:

要正确确定键盘高度并调整视图,请添加以下观察者:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil];

Then I use the following methods to animate the keyboard adjustment. Really all you need is the keyboardBounds object, but if you happen to be using AutoLayout, this is how you'd do it:

然后我使用以下方法为键盘调整设置动画。你真正需要的只是keyboardBounds对象,但是如果你正好使用AutoLayout,那么你就是这样做的:

- (void)keyboardDidShow:(NSNotification *)notification
{
    [self scrollControlBarTo:notification up:YES];
}

-(void)keyboardDidHide:(NSNotification *)notification
{
    [self scrollControlBarTo:notification up:NO];
}

- (void)scrollControlBarTo:(NSNotification *)notification up:(BOOL)up
{
    [_keyboardControlsBar layoutIfNeeded];
    CGRect keyboardBounds;
    NSDictionary *info = [notification userInfo];
    NSNumber *number = [info objectForKey:UIKeyboardAnimationDurationUserInfoKey];
    double duration = [number doubleValue];
    [[info objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardBounds];
    UIViewAnimationCurve curve = [[info objectForKey:UIKeyboardAnimationCurveUserInfoKey] integerValue];

    [UIView animateWithDuration:duration
                          delay:0
                        options:UIViewAnimationOptionBeginFromCurrentState
                     animations:^{
                         [UIView setAnimationCurve:curve];
                         _keyboardControlsBarBottomConstraint.constant = (up) ? keyboardBounds.size.height : 0;
                         [self.view layoutIfNeeded];
                     } completion:nil];
}