简体   繁体   中英

Keyboard hiding textfield in iPad

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.

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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