簡體   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