获取iOS键盘的高度而不显示键盘

获取iOS键盘的高度而不显示键盘

本文介绍了获取iOS键盘的高度而不显示键盘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图获得iOS键盘的高度。我已经完成并使用了涉及订阅通知的方法,例如详细信息:

I'm trying to get the height of the iOS keyboard. I've gone through and used the method involving subscribing to a notification such as detailed here:https://gist.github.com/philipmcdermott/5183731

- (void)viewDidAppear:(BOOL) animated {
    [super viewDidAppear:animated];
    // Register notification when the keyboard will be show
    [[NSNotificationCenter defaultCenter] addObserver:self
        selector:@selector(keyboardWillShow:)
        name:UIKeyboardWillShowNotification
        object:nil];

    // Register notification when the keyboard will be hide
    [[NSNotificationCenter defaultCenter] addObserver:self
        selector:@selector(keyboardWillHide:)
        name:UIKeyboardWillHideNotification
        object:nil];
}

- (void)keyboardWillShow:(NSNotification *)notification {
    CGRect keyboardBounds;

    [[notification.userInfo valueForKey:UIKeyboardFrameBeginUserInfoKey] getValue:&keyboardBounds];

    // Do something with keyboard height
}

- (void)keyboardWillHide:(NSNotification *)notification {
    CGRect keyboardBounds;

    [[notification.userInfo valueForKey:UIKeyboardFrameBeginUserInfoKey] getValue:&keyboardBounds];

    // Do something with keyboard height
}

这适用于用户实际显示键盘时。

我的问题:我有另一个视图,我们称之为micView,可能会显示在键盘出现之前。用户可以在键入之前选择使用麦克风。我希望micView与键盘的高度相同,这就是我需要键盘高度的原因,但是在键盘被迫出现之前我需要它。因此,在我需要读取高度值之前,未达到UIKeyboardWillShowNotification。

My problem: I have another view, let's call it micView, that may be presented before the keyboard appears. The user may choose to use the microphone before typing. I would like the micView to be the same height as the keyboard, which is why I need the keyboard's height, but I need it before the keyboard was forced to appear. Thus the UIKeyboardWillShowNotification is not reached before I need to read the value of the height.

我的问题是:如何通过Notifications或其他方法获得键盘的高度,而不会出现键盘。

My question is: how do I get the height of the keyboard through Notifications, or some other method without ever having the keyboard appear.

我考虑过明确强制键盘出现在viewDidLoad中,这样我就可以将一个实例变量设置为该值,然后将其隐藏并删除两者的动画。但这真的是唯一的方法吗?

I considered explicitly forcing the keyboard to appear in viewDidLoad, so that I can set an instance variable to that value, then hiding it and getting rid of the animations for both things. But is that really the only way to do that?

推荐答案

您可以使用的快速解决方案与您想要缓存键盘时使用的相同(第一次使用时)显示它,你会稍微延迟...)。该库是。有趣的位:

A quick solution that you could use, is the same one used when you want to cache the keyboard (the first time you show it, you get a slight delay...). The library is here. The interesting bits:

[[[[UIApplication sharedApplication] windows] lastObject] addSubview:field];
[field becomeFirstResponder];
[field resignFirstResponder];
[field removeFromSuperview];

所以基本上是显示它然后隐藏它。您可以收听通知,只需获得 height 而无需实际查看。 奖金:你可以缓存它。 :)

So basically is showing it and then hiding it. You could listen for notifications and just get the height without actually seeing it. Bonus: you get to cache it. :)

这篇关于获取iOS键盘的高度而不显示键盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-02 02:03