2013-11-01 20 views
11

Ich habe eine Ansicht auf meinem Storyboard, um Benutzer Login-Formular anzuzeigen, so sieht es so aus: Hauptansicht-> Bildlauf-> Inhaltsansicht-> zwei Textfelder und Login-Taste an der oben und eine Register-Schaltfläche am unteren Rand der Ansicht. Ich benutze das automatische Layout und die untere Taste hat den unteren Platzrand. Wenn ich auf das Textfeld und die Tastatur klicke, möchte ich in der Ansicht blättern, um die Größe in sichtbare rect zu ändern. Die Inhaltsgröße sollte jedoch weiterhin bis zum Register-Button reichen, aber die Schaltfläche wird nach oben verschoben, wenn sich die Größe der Ansicht ändert. Wie könnte ich machen was ich will?iOS AutoLayout mit Scrollview und Tastatur

Ich benutze diesen Code, wenn Tastatur erscheint:

- (void)keyboardWillShow:(NSNotification *)aNotification 
{ 
    NSDictionary *info = [aNotification userInfo]; 
    NSValue *kbFrame = [info objectForKey:UIKeyboardFrameEndUserInfoKey]; 
    NSTimeInterval animationDuration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue]; 
    CGRect keyboardFrame = [kbFrame CGRectValue]; 

    CGSize s = self.scrollView.contentSize; 
    CGFloat height = keyboardFrame.size.height; 
    self.scrollViewBottomLayoutConstraint.constant = height; 

    [UIView animateWithDuration:animationDuration animations:^{ 
     [self.view layoutIfNeeded]; 
     [self.scrollView setContentSize:s]; 
    }]; 
} 

Antwort

29

Versuchen Sie, den Inhalt Größe allein, verlassen und stattdessen die contentInset Eigenschaft des Scroll-Ansicht neu einstellen. Dann müssen Sie sich nicht mit Einschränkungen herumschlagen.

- (void)keyboardUp:(NSNotification *)notification 
{ 
    NSDictionary *info = [notification userInfo]; 
    CGRect keyboardRect = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue]; 
    keyboardRect = [self.view convertRect:keyboardRect fromView:nil]; 

    UIEdgeInsets contentInset = self.scrollView.contentInset; 
    contentInset.bottom = keyboardRect.size.height; 
    self.scrollView.contentInset = contentInset; 
} 
+0

Danke. Es klappt. –

+0

Große Antwort; funktioniert super für mich! Vielen Dank! –

+1

schön und einfach, aber ein Problem ist die Größe der Bildlaufleiste nicht auf den sichtbaren Teil der Scroll/Tabelle/Sammlung Ansicht. –

1

Der beste Weg, die ich gefunden habe ist NSLayoutConstraint wie so außer Kraft zu setzen:

@implementation NHKeyboardLayoutConstraint 

- (void) dealloc { 
    [[NSNotificationCenter defaultCenter] removeObserver:self]; 
} 

- (void) awakeFromNib { 
    [super awakeFromNib]; 
    [[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(keyboardWillChangeFrame:) 
               name:UIKeyboardWillChangeFrameNotification 
               object:nil]; 
} 

- (void)keyboardWillChangeFrame:(NSNotification *)notification { 

    CGRect endKBRect = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue]; 

    CGFloat animationDuration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue]; 
    CGRect frame = [UIApplication sharedApplication].keyWindow.bounds; 

    self.constant = frame.size.height - endKBRect.origin.y; 


    [UIView animateWithDuration:animationDuration animations:^{ 
     [[UIApplication sharedApplication].keyWindow layoutIfNeeded]; 
    }]; 
} 


@end 

dann in Ihrem xib zu dieser Klasse des Klassentyp der betreffenden Einschränkung ändern.

Verwandte Themen