在我的应用程序中,我想调整UITextView(self.message
)的大小,导航控制器的视图,并且想self.selectedMedia
在显示键盘时更改UIScrollView()的位置。为此,我正在使用此委托方法:
- (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]
基于给定的约束来调整高度?提交动画后是否应更新约束?还是我还有其他事情要忽略?
使用自动版式时,您不应该设置视图的框架。相反,您应该调整约束,通常是'constant'属性。
在您的情况下,您正在更改视图的高度,并且在此过程中打破了约束。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句