键盘隐藏在iPad中的文本字段

时间:2013-11-07 07:48:18

标签: ios uitextfield

您好我的问题是键盘隐藏了UITextField中的UIScrollView

为此我使用了苹果文档中的一些代码。

在ViewDidLoad

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardWasShown:)
                                             name:UIKeyboardDidShowNotification object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardWillBeHidden:)
                                             name:UIKeyboardWillHideNotification object:nil];

 - (void)keyboardWasShown:(NSNotification*)aNotification
 {
     int rowNumber=(selectetTxtfld.tag-1)/7;
     if (rowNumber>2) {
         NSDictionary* info = [aNotification userInfo];
         CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
         CGRect bkgndRect = selectetTxtfld.superview.frame;
         bkgndRect.size.height += kbSize.height;
         [selectetTxtfld.superview setFrame:bkgndRect];
         [scrlView setContentOffset:CGPointMake(0.0, selectetTxtfld.frame.origin.y-200) animated:YES];
     }

}

 - (void)keyboardWillBeHidden:(NSNotification*)aNotification
 {   
     UIEdgeInsets contentInsets = UIEdgeInsetsZero;
     scrlView.contentInset = contentInsets;
     scrlView.scrollIndicatorInsets = contentInsets;
     scrlView.contentOffset=CGPointZero;
}

现在它工作正常。 但我在代码行中听到了

  [scrlView setContentOffset:CGPointMake(0.0, selectetTxtfld.frame.origin.y-200) animated:YES];

我正在使用200键盘的高度。如果我这样使用苹果将拒绝该应用程序。是对还是没有。

我也试过这个代码。但是没有显示scrllview的文本字段和内容

    [scrlView setContentOffset:CGPointMake(0.0, selectetTxtfld.frame.origin.y-kbSize.height) animated:YES];

在我的应用中,我正在使用方向

 - (NSUInteger)supportedInterfaceOrientations
 {
     return UIInterfaceOrientationMaskLandscapeRight;
 }

请帮助我如何使用键盘高度。

1 个答案:

答案 0 :(得分:1)

获取键盘尺寸:

- (void) keyboardWasShown:(NSNotification *)nsNotification {
    NSDictionary *userInfo = [nsNotification userInfo];
    CGSize kbSize = [[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

    NSLog(@"Height: %f Width: %f", kbSize.height, kbSize.width);
    // Portrait:    Height: 264.000000  Width: 768.000000
    // Landscape:   Height: 1024.000000 Width: 352.000000
}