【问题标题】:The height on my soft keyboard changes when triggered multiple times多次触发时,我的软键盘上的高度会发生变化
【发布时间】:2020-03-02 14:45:59
【问题描述】:

我有一个观察到的方法,在显示soft keyboard 时触发。它工作正常,但由于某种原因,soft keyboardheight 在隐藏后会发生变化,然后第二次出现。我找不到原因,并且隐藏委托中似乎没有任何东西可以改变其价值。这是什么原因造成的?我已经通过存储高度然后第二次使用它来解决这个问题,但我想知道这个问题的原因。

- (void)keyboardWasShown:(NSNotification*)aNotification
{
    NSDictionary* info = [aNotification userInfo];

    CGSize keyboardSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

    CGRect visibleRect = self.view.frame;

    if (_storedKeyboardHeight.size.height == 0) {
        _storedKeyboardHeight.size.height = keyboardSize.height;
    }

    visibleRect.size.height = _storedKeyboardHeight.size.height;
    visibleRect.origin.y = self.view.height - visibleRect.size.height;

    CGRect rectOfCellInTableView = [self.loginTableView rectForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:1]];

    //This changes the second time
    NSLog(@"???? %f", keyboardSize.height);
    //so I store the original value here
    NSLog(@"???? %@", CGRectCreateDictionaryRepresentation(_storedKeyboardHeight));

    if ((rectOfCellInTableView.origin.y + rectOfCellInTableView.size.height) > visibleRect.origin.y){
        CGPoint scrollPoint = CGPointMake(0.0, (rectOfCellInTableView.origin.y + rectOfCellInTableView.size.height) - visibleRect.origin.y + 50);
        [self.loginTableView setContentOffset:scrollPoint animated:YES];
    }
}

第一次高度是291,第二次是233。

【问题讨论】:

    标签: ios objective-c nsnotificationcenter iphone-softkeyboard


    【解决方案1】:

    问题是你检查了错误的框架:

    UIKeyboardFrameBeginUserInfoKey
    

    当键盘被显示时,它在显示过程的开始处的帧高度是多少你并不关心。你想知道的是显示过程结束的帧:

    UIKeyboardFrameEndUserInfoKey
    

    此外,您似乎收到了错误的通知。您尚未显示您注册的通知,但您的方法名称keyboardWasShown 表明您在键盘显示时收到通知确实。为时已晚;这个通知几乎没有任何意义。您想知道键盘何时显示。

    【讨论】:

    • 非常好,非常感谢您的澄清!至于方法名称,我认为这并不重要,因为它在[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardWillShowNotification object:nil]; 中观察到,这只是不好的命名;)
    • 明白,正如我所说,您没有显示您注册的通知,所以我不得不根据名称猜测。 :)
    猜你喜欢
    • 2018-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-17
    • 1970-01-01
    • 2011-01-13
    • 1970-01-01
    相关资源
    最近更新 更多