繁体   English   中英

OBJ-C-返回错误的键盘高度?

[英]Obj-c - Wrong keyboard height is returned?

我正在尝试获取iOS键盘的高度,以便在为键盘设置动画时可以向上移动视图。 但是,我不断获得返回的值258(这是不正确的,因为它将我的视图推得太高了?)为什么会发生这种情况? 代码如下:

ViewController.m

-(void)viewDidLoad {

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

}
    - (void)keyboardWillChange:(NSNotification *)notification {

        NSDictionary* keyboardInfo = [notification userInfo];

        NSValue* keyboardFrameBegin = [keyboardInfo valueForKey:UIKeyboardFrameEndUserInfoKey];

        CGRect keyboardFrameBeginRect = [keyboardFrameBegin CGRectValue];
      self.keyboardHeight = keyboardFrameBeginRect.size.height;


    }

    - (void) animateTextView:(BOOL) up
    {

        const int movementDistance = self.keyboardHeight;

        const float movementDuration = 0.3f;
        int movement= movement = (up ? -movementDistance : movementDistance);


        [UIView beginAnimations: @"anim" context: nil];
        [UIView setAnimationBeginsFromCurrentState: YES];
        [UIView setAnimationDuration: movementDuration];

        self.upView.frame = CGRectOffset(self.upView.frame, 0, movement);
        [UIView setAnimationDidStopSelector:@selector(afterAnimationStops)];
        [UIView commitAnimations];

    }

要获得键盘的高度,应使用UIKeyboardWillShowNotificationUIKeyboardDidShowNotification代替UIKeyboardWillChangeFrameNotification.

-(void)viewDidLoad {

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChange:) name:UIKeyboardWillShowNotification object:nil];

}

如果使用UITabBarController ,则必须计算不带标签栏高度的框架。 您可以使用以下代码获取标签栏的高度。

self.tabBarController.tabBar.frame.height

您应该注册UIKeyboardWillShowNotification以便观察键盘高度。

1)  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChange:) name:UIKeyboardWillShowNotification object:nil];

2)您也可以尝试以下代码:

(void)keyboardWillChange:(NSNotification *)notification 
{
    NSDictionary* keyboardInfo = [notification userInfo];
    NSValue* keyboardFrameBegin = [keyboardInfo valueForKey:UIKeyboardFrameBeginUserInfoKey];
    CGRect keyboardFrameBeginRect = [keyboardFrameBegin CGRectValue];
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM