gpt4 book ai didi

ios - 何时在 iOS 7 中使用 AutoLayout 更新约束

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:08:00 25 4
gpt4 key购买 nike

在我的应用程序中,我想调整 UITextView (self.message)、Navigation Controller 的 View 的大小,并且我想更改 UIScrollView (self.selectedMedia) 的位置当键盘显示。为此,我使用了这个委托(delegate)方法:

- (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] 不是根据给定的约束调整高度吗?提交动画后是否应该更新约束?或者还有其他我忽略的东西吗?

最佳答案

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

在您的情况下,您正在更改 View 的高度并在此过程中打破约束。

关于ios - 何时在 iOS 7 中使用 AutoLayout 更新约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19318086/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com