Beim aktualisieren von constraints mit AutoLayout in iOS 7
In meiner Anwendung möchte ich die Größe einer UITextView (self.message
), Navigation Controller-Ansicht, und ich möchten, ändern Sie die position eines UIScrollView ( self.selectedMedia
), wenn die Tastatur zeigt. Um dies zu erreichen, bin ich mit dieser Delegat-Methode:
- (void)keyboardWillShow:(NSNotification *)notification
{
NSDictionary* userInfo = [notification userInfo];
NSTimeInterval animationDuration;
UIViewAnimationCurve animationCurve;
CGRect keyboardFrame;
CGRect navigationControllerFrame = self.navigationController.view.frame;
CGRect textViewFrame = self.message.frame;
CGFloat keyboardHeight;
[[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&animationCurve];
[[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&animationDuration];
[[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] getValue:&keyboardFrame];
keyboardHeight = keyboardFrame.size.height;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:animationDuration];
[UIView setAnimationCurve:animationCurve];
[self.navigationController.view setFrame:CGRectMake(
navigationControllerFrame.origin.x,
navigationControllerFrame.origin.y,
navigationControllerFrame.size.width,
navigationControllerFrame.size.height - keyboardHeight
)];
[self.message setFrame:CGRectMake(
textViewFrame.origin.x,
textViewFrame.origin.y,
textViewFrame.size.width,
textViewFrame.size.height - keyboardHeight)];
if (self.selectedMedia.hidden == NO) {
[self.selectedMedia setFrame:CGRectMake(
self.selectedMedia.frame.origin.x,
self.selectedMedia.frame.origin.y - keyboardHeight,
self.selectedMedia.frame.size.width,
self.selectedMedia.frame.size.height
)];
}
[self.navigationController.view updateConstraints];
[UIView commitAnimations];
}
Wenn ich diesen code ausführen, bekomme ich die Fehlermeldung, dass die constraints nicht erfüllt werden kann:
2013-10-11 14:03:52.532 PoC[78199:a0b] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
(
"<NSLayoutConstraint:0xe5502d0 V:[UITextView:0xf26da00(380)]>",
"<NSLayoutConstraint:0x8b93620 V:|-(0)-[UITextView:0xf26da00] (Names: '|':UIView:0x8b978f0 )>",
"<NSLayoutConstraint:0x8b984c0 V:[UIScrollView:0x8b8f570]-(0)-| (Names: '|':UIView:0x8b978f0 )>",
"<NSLayoutConstraint:0x8b98520 V:[UITextView:0xf26da00]-(0)-[UIScrollView:0x8b8f570]>",
"<NSAutoresizingMaskLayoutConstraint:0x8b9acf0 h=--& v=--& V:[UIView:0x8b978f0(244)]>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0xe5502d0 V:[UITextView:0xf26da00(380)]>
Ich Frage mich, warum die UITextView hat immer noch eine Höhe von 380. Nicht die Aussage [self.navigationController updateConstraints]
stellen Sie die Höhe auf der Basis der gegebenen Randbedingungen? Sollten die Einschränkungen werden aktualisiert, nachdem die Begehung der animation? Oder ist es etwas anderes, das ich im Blick?
InformationsquelleAutor Martin de Keijzer | 2013-10-11
Du musst angemeldet sein, um einen Kommentar abzugeben.
Bei der Verwendung von Autolayout sollten Sie nicht die Einstellung der Ansicht-Rahmen. Stattdessen stellen Sie die Einschränkungen in der Regel die 'Konstanten' - Eigenschaft.
In deinem Fall, ändern Sie die Höhe einer Ansicht und in den Prozess bricht eine Einschränkung.
setFrame
fürself.message
. Vom storyboard erstellte ich eine Eigenschaft, die ich angebracht, um die Höhe Einschränkung der UITextView (messageHeightConstraint
). Danach habe ich noch eine Anweisung zum aktualisieren der constraint-Konstante:self.messageHeightConstraint.constant = self.messageHeightConstraint.constant - keyboardHeight;
Job gut gemacht! Danke!!!InformationsquelleAutor Nikola Lajic
Beachten, dass, wenn ich verstanden habe, dein problem genau, aber die scroll-view ist etwas ganz besonderes in Bezug auf autolayout (siehe technote https://developer.apple.com/library/ios/technotes/tn2154/)
InformationsquelleAutor Laurent Cerveau