iOS自动布局难题 - 初始视图失败,但在旋转手机后工作



我是iOS编程的新手,正在开发用Objective C和C++编写的旧版应用程序。到目前为止,它还没有约束,我的任务是修复一个错误,这里的每个人都同意最好通过约束和自动布局来解决。没有一个屏幕是使用界面生成器构建的,切换到界面生成器超出了我当前任务的范围。

如主题中所述,我的问题是我有一个屏幕,在第一次打开时会弹出错误:

2017-06-09 07:35:58.474149 Mobile[2132:1766896] [LayoutConstraints] 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. 
(
"<NSLayoutConstraint:0x1899dd30 V:|-(50)-[TPKeyboardAvoidingScrollView:0x17ba9a00]   (active, names: '|':UIScrollView:0x17f88200 )>",
"<NSLayoutConstraint:0x1899e310 V:|-(0)-[TPKeyboardAvoidingScrollView:0x17ba9a00]   (active, names: '|':UIScrollView:0x17f88200 )>"
)
Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x1899dd30 V:|-(50)-[TPKeyboardAvoidingScrollView:0x17ba9a00]   (active, names: '|':UIScrollView:0x17f88200 )>
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.

如果我忽略这一点并简单地将屏幕旋转到横向,我就不会再收到错误消息并且屏幕显示正常。然后我可以旋转回纵向,屏幕仍然正确显示,没有错误消息。

我确信这是我的约束问题,但我无法弄清楚我做错了什么。

UIScrollView * scrView;
for (UIView* view in scrollView.subviews) {
if ([view isKindOfClass:[TPKeyboardAvoidingScrollView class]]) {
scrView = (UIScrollView *)view;
[scrollView setContentSize:scrView.contentSize];
break;
}
}
[scrView removeConstraints:scrView.constraints];
scrView.translatesAutoresizingMaskIntoConstraints = NO;
[scrollView addConstraint:[NSLayoutConstraint
constraintWithItem:scrView
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:scrollView
attribute:NSLayoutAttributeTop
multiplier:1.0
constant:0]];
[scrollView addConstraint:[NSLayoutConstraint
constraintWithItem:scrView
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:scrollView
attribute:NSLayoutAttributeBottom
multiplier:1.0
constant:0]];
[scrollView addConstraint:[NSLayoutConstraint
constraintWithItem:scrView
attribute:NSLayoutAttributeLeft
relatedBy:NSLayoutRelationEqual
toItem:scrollView
attribute:NSLayoutAttributeLeft
multiplier:1.0
constant:0]];
[scrollView addConstraint:[NSLayoutConstraint
constraintWithItem:scrView
attribute:NSLayoutAttributeRight
relatedBy:NSLayoutRelationEqual
toItem:scrollView
attribute:NSLayoutAttributeRight
multiplier:1.0
constant:0]];

我也确信我的部分问题是我试图将新的约束硬塞到一个主要使用iOS 7之前编程设置框架的应用程序中。

任何人都可以从我发布的代码片段中指出我的错误,或者至少指出我自己诊断错误的好资源吗?

是否仅在此代码片段中添加约束? 以下消息表示滚动视图有两个相互冲突的约束。其中一个试图将 50pt 空间设置为顶部,另一个试图将 0pt 空间添加到顶部。系统不知道要使用哪个约束。

"<NSLayoutConstraint:0x1899dd30 V:|-(50)-[TPKeyboardAvoidingScrollView:0x17ba9a00]   (active, names: '|':UIScrollView:0x17f88200 )>",
"<NSLayoutConstraint:0x1899e310 V:|-(0)-[TPKeyboardAvoidingScrollView:0x17ba9a00]   (active, names: '|':UIScrollView:0x17f88200 )>"

尝试使用

[scrollView layoutIfNeeded] 

删除约束后。