В моем приложении я хочу изменить размер представления UITextView (self.message
), навигационного контроллера, и я хочу изменить положение UIScrollView (self.selectedMedia
), когда на клавиатуре отображается. Для достижения этой цели я использую этот метод делегата:Когда обновлять ограничения с помощью AutoLayout в iOS 7
- (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];
}
Когда я запускаю этот код, я получаю сообщение об ошибке, что ограничения не могут быть удовлетворены:
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)]>
Интересно, почему UITextView все еще имеет высота 380. Не указывает ли инструкция [self.navigationController updateConstraints]
высоту, основанную на данных ограничениях? Должны ли обновления обновляться после совершения анимации? Или есть что-то еще, что я забываю?
Вы так правы, я удалил 'setFrame' для' self.message'. Из раскадровки я создал свойство, которое я привязал к ограничению высоты UITextView ('messageHeightConstraint'). После этого я добавил инструкцию для обновления константы ограничения: 'self.messageHeightConstraint.constant = self.messageHeightConstraint.constant - keyboardHeight;' Работа выполнена! Спасибо! –