Sto cercando di spostare un UIScrollView
quando la tastiera nasconde un UITextField
modificando le dimensioni utilizzando lo contentInsets
come mostrato.Contenuto UIScrollView Inserti non funzionanti per Altezza tastiera
Tuttavia, non funziona per l'altezza della tastiera. L'altezza della tastiera arriva a 216, ma smette di scorrere solo nella posizione corretta se imposto il margine inferiore su 515 per la modalità verticale iPhone e 310 per la modalità orizzontale iPhone. Perché queste dimensioni sarebbero così diverse? Io non voglio hardcode questi valori arbitrari in
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.frame = self.parentViewController.view.frame;
[self.textView becomeFirstResponder];
NSLog(@"scrollview: %f,%f, parent: %f,%f", self.view.frame.size.height, self.view.frame.size.width, self.parentViewController.view.frame.size.height, self.parentViewController.view.frame.size.width);
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
}
- (void)keyboardWasShown:(NSNotification *)notification
{
if([[UIDevice currentDevice]userInterfaceIdiom]==UIUserInterfaceIdiomPhone) {
// Step 1: Get the size of the keyboard.
CGFloat keyboardHeight = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size.height;
// Step 2: Adjust the bottom content inset of your scroll view by the keyboard height.
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardHeight, 0.0);
((UIScrollView*)self.view).contentInset = contentInsets;
((UIScrollView*)self.view).scrollIndicatorInsets = contentInsets;
// Step 3: Scroll the target text field into view.
CGRect aRect = self.view.frame;
aRect.size.height = aRect.size.height - keyboardHeight;
if (!CGRectContainsPoint(aRect, self.textView.frame.origin)) {
CGPoint scrollPoint = CGPointMake(0.0, self.textView.frame.origin.y - keyboardHeight);
[((UIScrollView*)self.view) setContentOffset:scrollPoint animated:YES];
}
}
}
- (void) keyboardWillHide:(NSNotification *)notification {
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
((UIScrollView*)self.view).contentInset = contentInsets;
((UIScrollView*)self.view).scrollIndicatorInsets = contentInsets;
}
Edit:.
Prima che la tastiera è aperta, a stampare questo fuori:
NSLog(@"scrollview: %f,%f, parent: %f,%f", self.view.frame.size.height, self.view.frame.size.width, self.parentViewController.view.frame.size.height, self.parentViewController.view.frame.size.width);
e stampe questo:
scrollview: 431.000000,320.000000, parent: 431.000000,320.000000
sta scorrendo bene senza tastiera, potrebbe essere possibile avere un'altezza superiore all'altezza della vista padre. –
Sì. Scorre bene – Kristin
voglio conoscere il frame di scrollview e il suo frame di visualizzazione genitore prima di aprire la tastiera. –