简体   繁体   English

测试键盘在显示时是否隐藏文本字段

[英]Test whether keyboard hide a text field when it's shown

I try to move up the form when the keyboard is shown, my approach is to test whether the frame of the keyboard and the frame of the text field intersects. 当显示键盘时,我尝试将表格向上移动,我的方法是测试键盘的框架和文本字段的框架是否相交。

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


    // Get the size of the keyboard.
    CGRect keyboardFrame = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];

    //Test whether the current frame of the text field is hidden by the keyboard

    if (!CGRectIsNull(CGRectIntersection(keyboardFrame,self.activeField.frame))) {
        NSLog(@"Key board frame intersects with the text field frame");
    }


}

In the code above, CGRectIsNull always return null. 在上面的代码中, CGRectIsNull始终返回null。

A debugging statements return me these info about the keyboard and the active text field being selected in the form: 一条调试语句将以下有关键盘和活动文本字段的信息返回给我:

Keyboard size = (width=352, height=1024) keyboard origin = (x=-352, y=0) 键盘大小=(宽度= 352,高度= 1024)键盘原点=(x = -352,y = 0)

key board frame = (-352,0,352,1024) Text field frame = (200, 15, 300, 30) 键盘框架=(-352,0,352,1024)文本字段框架=(200、15、300、30)

Every text field has the same frame values, which means something is wrong. 每个文本字段都具有相同的框架值,这意味着有问题。 So how can I test whether the keyboard is hiding the text field so that I move my form up and down. 因此,如何测试键盘是否隐藏了文本字段,以便我上下移动表单。 Thanx. 谢谢

I'd put the entire thing in a fullscreen UIScrollView and then resize it when needed... 我会将整个东西放到全屏UIScrollView中,然后在需要时调整其大小...

// This in your init somewhere
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChange:) name:UIKeyboardWillChangeFrameNotification object:nil];

-(void) keyboardWillChange:(NSNotification*)notify {

    CGRect endFrame;
    float duration = [[[notify userInfo] valueForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
    [[[notify userInfo] valueForKey:UIKeyboardFrameEndUserInfoKey] getValue:&endFrame];
    endFrame = [self.view convertRect:endFrame fromView:nil];
    float y = (endFrame.origin.y > self.view.bounds.size.height ? self.view.bounds.size.height : endFrame.origin.y);

    [UIView animateWithDuration:duration animations:^{
        scrollView.frame = CGRectMake(0, 0, self.view.bounds.size.width, y);
    }];

}

Try this: 尝试这个:

 [[NSNotificationCenter defaultCenter] addObserverForName:UIKeyboardWillShowNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note) {}];

And use info from [note userInfo]; 并使用[note userInfo];

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

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