Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
何时在iOS 7中使用自动布局更新约束_Ios_Objective C_Uikit_Autolayout - Fatal编程技术网

何时在iOS 7中使用自动布局更新约束

何时在iOS 7中使用自动布局更新约束,ios,objective-c,uikit,autolayout,Ios,Objective C,Uikit,Autolayout,在我的应用程序中,我想调整UITextView(self.message)、导航控制器视图的大小,并想在键盘显示时更改UIScrollView(self.selectedMedia)的位置。要实现这一点,我将使用此委托方法: - (void)keyboardWillShow:(NSNotification *)notification { NSDictionary* userInfo = [notification userInfo]; NSTimeInterval animationDura

在我的应用程序中,我想调整UITextView(
self.message
)、导航控制器视图的大小,并想在键盘显示时更改UIScrollView(
self.selectedMedia
)的位置。要实现这一点,我将使用此委托方法:

- (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)]>
2013-10-11 14:03:52.532 PoC[78199:a0b]无法同时满足约束条件。
可能下面列表中至少有一个约束是您不想要的。试着这样做:(1)看看每个约束,试着找出你不期望的约束;(2) 找到添加了不需要的约束的代码,然后修复它。(注意:如果您看到不理解的NSAutoresizingMaskLayoutConstraints,请参阅UIView属性TranslatesAutoResizingMaskToConstraints的文档)
(
"",
"",
"",
"",
""
)
将尝试通过打破约束进行恢复

我想知道为什么UITextView的高度仍然是380。语句
[self.navigationController updateConstraints]
是否根据给定的约束调整高度?提交动画后是否应更新约束?或者还有什么我忽略了的吗?

请注意,如果我完全理解您的问题,当然,但是滚动视图在自动布局方面非常特殊(请参阅技术说明)

使用自动布局时,您不应该设置视图的框架。相反,您应该调整约束,通常是“常量”属性


在您的例子中,您正在更改视图的高度,并在这个过程中打破了一个约束。

您说得很对,我删除了
self.message的
设置框架。从情节提要中,我创建了一个属性,该属性附加到UITextView的高度约束(
messageHeightConstraint
)。之后,我添加了一条语句来更新约束常量:
self.messageHeightConstraint.constant=self.messageHeightConstraint.constant-keyboardHeight做得好!非常感谢。