0

我尝试在显示键盘时向上移动表单,我的方法是测试键盘的框架和文本字段的框架是否相交。

- (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");
    }


}

在上面的代码中,CGRectIsNull总是返回 null。

调试语句返回有关键盘和在表单中选择的活动文本字段的这些信息:

键盘尺寸 = (width=352, height=1024) 键盘原点 = (x=-352, y=0)

关键板框架 = (-352,0,352,1024) 文本字段框架 = (200, 15, 300, 30)

每个文本字段都有相同的帧值,这意味着有问题。那么如何测试键盘是否隐藏了文本字段,以便我上下移动表单。谢谢。

4

2 回答 2

0

我会把整个东西放在全屏 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);
    }];

}
于 2013-02-28T14:55:19.067 回答
-1

尝试这个:

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

并使用来自的信息[note userInfo];

于 2013-02-28T14:58:15.397 回答