2016-04-06 4 views
0

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.

Document Outline

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.

Result

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 Expectation

Vielen Dank wollte, ein kleiner Hinweis würde geschätzt.

Antwort

0

Gelöst

ich dieses Problem gelöst durch seinen Inhalt, indem Sie einige Nummer nach innen versetzt verwalten. In keyboardWasShown:, fügte ich seinen Inhalt nach Höhe meines Textfelds und Schaltfläche eingefügt. Sagen wir, es war insgesamt 100, also das ist es.

UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height+100, 0.0); 

Vielen Dank.

2

Die einfachste Lösung wäre, Ihre Ansicht (oder Scrollview) nach dem Öffnen der Tastatur zu verschieben.

- (void)keyboardWillShow:(NSNotification*)notification{ 
    [self.view setFrame:CGRectMake(0,-100, self.view.frame.size.width, self.view.frame.size.height)]; // where 100 is the offset 
    [self.view setNeedsDisplay]; 

} 

- (void)keyBoardWillHide:(NSNotification*)notification{ 
    [self.view setFrame:CGRectMake(0,0, self.view.frame.size.width, self.view.frame.size.height)]; 
    [self.view setNeedsDisplay]; 
}