ios - 自动布局和 UIScrollview 以及 subview Controller 问题

标签 ios objective-c uiviewcontroller uiscrollview autolayout

在我的应用程序中,我有一个嵌入式 subview Controller ,在运行时我必须根据来自服务器的数据向其中添加一个或多个“Editor UIView”。我可以将每个“编辑器”建模为 xib 文件中的完整 UIViewController 并在运行时添加它。 “编辑器”的父级有一些自己的项目(如标题和几个按钮),因此我尝试使用单个子 UIViewController 构建分层方法,并将“编辑器”动态加载到其 UIScrollview 中。但是我无法让 Autolayout 配合。该图显示了基本安排。 enter image description here

问题始于将“内容 View ”嵌入到 UIScrollview 中。我可以让内容 View 与 ScrollView 一起工作。当我添加编辑器 View 并向内容 View 添加运行时约束时,我收到多个自动布局投诉,而且 ScrollView 内容大小为 0,0,所以我显然没有得到我需要的东西。

关于如何处理这个问题有什么想法吗?我总是可以简单地为每个编辑器复制子 Controller 的额外项目,但以这种分层方式进行这项工作会很好。

请注意,我首先使用 UITableViewController 完成此操作,其中每个编辑器都是一个 UITableViewCell 并且喜欢自动布局,但我想看看我是否可以在没有表格的情况下完成此操作。也许我会回到那个。

在运行时添加了这些:

_editor = [self editorForDataType];
[_contentView addSubview:_editor];

NSLayoutConstraint *leftConstraint = [NSLayoutConstraint constraintWithItem:_editor
                                                                  attribute:NSLayoutAttributeLeading
                                                                  relatedBy:0
                                                                     toItem:self.contentView
                                                                  attribute:NSLayoutAttributeLeft
                                                                 multiplier:1.0
                                                                   constant:0];
[self.contentView addConstraint:leftConstraint];

NSLayoutConstraint *rightConstraint = [NSLayoutConstraint constraintWithItem:_editor
                                                                   attribute:NSLayoutAttributeTrailing
                                                                   relatedBy:0
                                                                      toItem:self.contentView
                                                                   attribute:NSLayoutAttributeRight
                                                                  multiplier:1.0
                                                                    constant:0];
[self.contentView addConstraint:rightConstraint];

NSLayoutConstraint *topConstraint = [NSLayoutConstraint constraintWithItem:_editor
                                                                  attribute:NSLayoutAttributeLeading
                                                                  relatedBy:0
                                                                     toItem:self.contentView
                                                                  attribute:NSLayoutAttributeTop
                                                                 multiplier:1.0
                                                                   constant:0];
[self.contentView addConstraint:topConstraint];

NSLayoutConstraint *bottomConstraint = [NSLayoutConstraint constraintWithItem:_editor
                                                                   attribute:NSLayoutAttributeTrailing
                                                                   relatedBy:0
                                                                      toItem:self.contentView
                                                                   attribute:NSLayoutAttributeBottom
                                                                  multiplier:1.0
                                                                    constant:0];
[self.contentView addConstraint:bottomConstraint];

最佳答案

最后,我需要的关键是以下一组约束,再加上在 viewDidLayoutSubviews 中添加编辑器 View 的约束并保存内容 View 高度的属性。

viewDidLoad 中,我将 height 属性设置为编辑器 View 的高度。现在效果很好。

enter image description here

关于ios - 自动布局和 UIScrollview 以及 subview Controller 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30196016/

相关文章:

ios - Fabric.with Twitter 无故崩溃

c# - GetViewForHeader 在模拟器中工作但在设备上不工作

ios - 如果用户按下后退按钮,如何调用一些代码

ios - 关闭两个 UIViewController

IOS UINavigationController、pushViewController 不工作

java - 我可以在 Xcode 项目中使用 .java 文件中的方法吗?

iphone - 在 Cocos-2d 中使用 slider 或进度控件控制动画

iphone - 我的 iPhone Objective-C 代码如何收到 UIWebView 中 Javascript 错误的通知?

objective-c - 尝试捕捉不起作用

swift - 从 UNUserNotificationCenterDelegate 呈现 ViewController