Per un'applicazione che sto progettando per l'iPad, ho una vista di scorrimento con alcuni campi di testo/viste di testo. Per mantenere tutto visibile, aggiusto la proprietà contentSize
della vista di scorrimento per aggiungere un buffer nella parte inferiore che corrisponda a quanto la tastiera si sovrappone alla vista di scorrimento. Ecco il codice (ci sono alcune cose specifiche per applicazione qui, ma si spera non così tanto che non si può dare un senso di esso):Come ottenere la dimensione della tastiera su schermo dell'iPad dopo la rotazione
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[nc addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc removeObserver:self name:nil object:nil];
}
- (void)keyboardWillShow:(NSNotification *)aNotification
{
NSValue *animationCurve = [[aNotification userInfo] valueForKey:UIKeyboardAnimationCurveUserInfoKey];
UIViewAnimationCurve curve;
[animationCurve getValue:&curve];
NSValue *animationDuration = [[aNotification userInfo] valueForKey:UIKeyboardAnimationDurationUserInfoKey];
NSTimeInterval duration;
[animationDuration getValue:&duration];
NSValue *endingFrame = [[aNotification userInfo] valueForKey:UIKeyboardFrameEndUserInfoKey];
CGRect frame;
[endingFrame getValue:&frame];
[UIView beginAnimations:@"keyboardWillShow" context:bodyView];
[UIView setAnimationCurve:curve];
[UIView setAnimationDuration:duration];
// Re-draw code here.
[UIView commitAnimations];
}
- (void)keyboardWillHide:(NSNotification *)aNotification
{
NSValue *animationCurve = [[aNotification userInfo] valueForKey:UIKeyboardAnimationCurveUserInfoKey];
UIViewAnimationCurve curve;
[animationCurve getValue:&curve];
NSValue *animationDuration = [[aNotification userInfo] valueForKey:UIKeyboardAnimationDurationUserInfoKey];
NSTimeInterval duration;
[animationDuration getValue:&duration];
[UIView beginAnimations:@"keyboardWillHide" context:bodyView];
[UIView setAnimationCurve:curve];
[UIView setAnimationDuration:duration];
// Re-draw code here
[UIView commitAnimations];
}
mia domanda è questa: cosa devo fare circa le dimensioni della tastiera durante la rotazione? Non ricevo notifiche sulla tastiera quando l'iPad viene ruotato, ma le dimensioni della tastiera cambiano in modo significativo. Idealmente avrei semplicemente regolato l'altezza della proprietà contentSize
in base alla quantità di sovrapposizione della tastiera in modalità orizzontale, ma non riesco a vedere un buon modo per farlo senza l'hard-coding dell'altezza della tastiera in entrambi gli orientamenti, cosa che non voglio fare