Ich möchte meine Ansicht automatisch nach oben bewegen, wenn die Tastatur angezeigt wird. Verwenden Sie bereits Apples Code here und es funktioniert gut.Sichtbarkeit des UIScrollView-Bereichs automatisch verschieben
So verwalte ich mein Objekt, also erstelle ich eine UIScrollView
, die UIView
abdeckt. Diese UIView
besteht aus UITextField
und UIButton
.
Dies ist, wie ich meine Ansicht neu einstellen, wenn Tastatur erscheint.
#pragma mark - Keyboard Handling
// Call this method somewhere in your view controller setup code.
- (void)registerForKeyboardNotifications
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillBeHidden:)
name:UIKeyboardWillHideNotification object:nil];
}
// Called when the UIKeyboardDidShowNotification is sent.
- (void)keyboardWasShown:(NSNotification*)aNotification
{
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
_scrollView.contentInset = contentInsets;
_scrollView.scrollIndicatorInsets = contentInsets;
// If active text field is hidden by keyboard, scroll it so it's visible
// Your app might not need or want this behavior.
CGRect aRect = self.view.frame;
aRect.size.height -= kbSize.height;
if (!CGRectContainsPoint(aRect, _mainView.frame.origin)) {
[self.scrollView scrollRectToVisible:_mainView.frame animated:YES];
}
}
// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
_scrollView.contentInset = contentInsets;
_scrollView.scrollIndicatorInsets = contentInsets;
}
Aber ich denke, es gibt einen Punkt, der das komisch macht. Wenn die Tastatur angezeigt wird, scrollt es und meine UITextField
wird sichtbar. Aber ich dachte, es wäre zu eng.
Meiner Meinung nach wäre es ein wenig besser, wenn mein UITextField
nach oben bewegt werden. Meine Frage ist, wie kann ich die Scroll-Sichtbarkeit einstellen? Es sieht aus wie eine Variable mit einer Konstanten
CGRect aRect = self.view.frame;
aRect.size.height -= kbSize.height;
if (!CGRectContainsPoint(aRect, _mainView.frame.origin)) {
[self.scrollView scrollRectToVisible:_mainView.frame animated:YES];
}
Hinweis hier hinzugefügt werden soll: Ergebnis, dass ich
Vielen Dank wollte, ein kleiner Hinweis würde geschätzt.