Keyboard hiding textfield in iPad

不问归期 提交于 2019-12-13 04:55:11

问题


Hi I had a problem that keyboard hiding UITextField in UIScrollView.

For that I used some code from apple documents.

In ViewDidLoad

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

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

and

 - (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];
     }

}

and

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

now it is working fine. But I heard that in code line

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

for the height of keyboard I am using 200.If I used like this the apple will reject the app. Is that right or not.

I tried this code also. But not showing the textfields and content of the scrllview

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

in my app I am using the orientation

 - (NSUInteger)supportedInterfaceOrientations
 {
     return UIInterfaceOrientationMaskLandscapeRight;
 }

So please help me how to use the key board height.


回答1:


To obtain keyboard dimension:

- (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
}


来源:https://stackoverflow.com/questions/19830496/keyboard-hiding-textfield-in-ipad

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!