我有一个自定义的UIView子类,它正通过NIB初始化.

-awakeFromNib中,我正在创建子视图,并try 将其置于其SuperView的中心.

[self setInteralView: [[UIView alloc] init]];
[[self internalView] addConstraint: [NSLayoutConstraint constraintWithItem: [self internalView]
                                                                 attribute: NSLayoutAttributeCenterX
                                                                 relatedBy: NSLayoutRelationEqual
                                                                    toItem: self
                                                                 attribute: NSLayoutAttributeCenterX
                                                                multiplier: 1
                                                                  constant: 0]];

这会中断,并导致以下输出:

2013-08-11 17:58:29.628 MyApp[32414:a0b] The view hierarchy is not prepared for the constraint: <NSLayoutConstraint:0xc1dcc80 UIView:0xc132a40.centerX == MyView:0xc1315a0.centerX>
    When added to a view, the constraint's items must be descendants of that view (or the view itself). This will crash if the constraint needs to be resolved before the view hierarchy is assembled. Break on -[UIView _viewHierarchyUnpreparedForConstraint:] to debug.
2013-08-11 17:58:29.630 MyApp[32414:a0b] View hierarchy unprepared for constraint.
    Constraint: <NSLayoutConstraint:0xc1dcc80 UIView:0xc132a40.centerX == MyView:0xc1315a0.centerX>
    Container hierarchy: 
<UIView: 0xc132a40; frame = (0 0; 0 0); clipsToBounds = YES; layer = <CALayer: 0xc132bc0>>
    View not found in container hierarchy: <MyView: 0xc1315a0; frame = (-128 -118; 576 804); layer = <CALayer: 0xc131710>>
    That view's superview: <UIView: 0xc131a70; frame = (0 0; 320 568); autoresize = W+H; layer = <CALayer: 0xc131b50>>

推荐答案

如错误所述,必须将约束添加到视图中,以便约束中涉及的视图是要将其添加到的视图或该视图的子视图.对于上面的代码,应该将该约束添加到self,而不是[self internalView].不工作的原因是在约束(self)中涉及的视图之一(如果您只考虑[self internalView]和以下)不在视图层次 struct 中.

有关更多详细信息,请参阅addConstraint:方法上的discussion section of the documentation.

以下是您的代码行,按照我的建议进行了修复:

UIView* internalView = [[UIView alloc] initWithFrame:CGRectZero];
internalView.translatesAutoresizingMaskIntoConstraints = NO;
[self setInternalView:internalView];

[self addConstraint: [NSLayoutConstraint constraintWithItem: [self internalMapView]
                                         attribute: NSLayoutAttributeCenterX
                                         relatedBy: NSLayoutRelationEqual
                                         toItem: self
                                         attribute: NSLayoutAttributeCenterX
                                         multiplier: 1
                                         constant: 0]];

Ios相关问答推荐

PieChartView未以编程方式显示在屏幕上

带外部笔划的圆形进度视图

Swift导航远离视频导致崩溃

iOS 16.4,SwiftUI 底部工作表有时会忽略presentationDetents

如何在 SwiftUI 中同时使用 LongPressGesture 和 ScrollView 中的滚动?

无法从 Xcode 获取模拟器列表.请打开 Xcode 并try 直接从那里运行项目以解决剩余问题

使用 Foundation 解压缩文件

工具栏底部 iOS 16 - SwiftUI

通过 Rosetta 打开模拟器 xcode 14 以修复滚动

Flutter:无法构建 iOS 应用程序ARCHIVE FAILED,为设备归档时遇到错误

UITableView 页脚,停止浮动内容

是否可以在没有 Mac 的情况下为 iOS 制作 PhoneGap 应用程序?

为什么 Safari 在使用 iOS 6 设备进行远程调试时显示No Inspectable Applications?

iOS 10 / Xcode 8 设备上的 NSLog 似乎被截断了?为什么?

在滚动视图中使用动态大小的控制器调整容器视图的大小

iPhone 未连接.连接 iPhone 后 Xcode 将继续

IOS - 从 UITextView 中删除所有填充

有什么方法可以加粗 NSString 的一部分?

如何进行键值观察并在 UIView 的框架上获取 KVO 回调?

Twitter api 文本字段值被截断