Pat*_*ini 81 objective-c ios autolayout uiview-hierarchy nslayoutconstraint
我有一个自定义的UIView子类,它通过一个nib进行初始化.
在-awakeFromNib,我正在创建一个子视图,并试图将其置于超级视图中.
[self setInteralView: [[UIView alloc] init]];
[[self internalView] addConstraint: [NSLayoutConstraint constraintWithItem: [self internalView]
attribute: NSLayoutAttributeCenterX
relatedBy: NSLayoutRelationEqual
toItem: self
attribute: NSLayoutAttributeCenterX
multiplier: 1
constant: 0]];
Run Code Online (Sandbox Code Playgroud)
这会中断,并导致以下输出:
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>>
Run Code Online (Sandbox Code Playgroud)
Cha*_* A. 93
在错误状态下,您必须将约束添加到视图中,以使约束中涉及的视图是您要将其添加到的视图或该视图的子视图.对于上面的代码,您应该将该约束添加到self,而不是[self internalView].它不能像写的那样工作的原因之一self是,如果你只考虑[self internalView]下面的那个,那么约束()中涉及的一个视图不在视图层次结构中.
有关更多详细信息,请参阅有关该方法的文档的讨论部分addConstraint:.
这是你的代码行,修正如我所说:
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]];
Run Code Online (Sandbox Code Playgroud)
Swi*_*ect 36
简答:交换父母和子女的观点.
假设self是父视图:
坏的:[subview addConstraint [NSLayoutConstraint constraintWithItem:self...
好的:[self addConstraint [NSLayoutConstraint constraintWithItem:subview...
迅速
subview.translatesAutoresizingMaskIntoConstraints = false
self.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat(
"H:|-0-[subview]-0-|",
options: .DirectionLeadingToTrailing,
metrics: nil,
views: ["subview":subview]))
Run Code Online (Sandbox Code Playgroud)
OBJ-C
[subview setTranslatesAutoresizingMaskIntoConstraints:NO];
[self addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat:@"H:|-0-[subview]-0-|"
options:NSLayoutFormatDirectionLeadingToTrailing
metrics:nil
views:NSDictionaryOfVariableBindings(subview)]];
Run Code Online (Sandbox Code Playgroud)
Kev*_*vin 10
其他提示:
我有一个新开发的应用程序在iOS7上正常运行但在iOS8上出错"视图层次结构没有为约束做好准备".
阅读其他帖子说,需要在创建约束之前添加(子)视图.我做到了但仍然得到了错误.
然后我发现我应该在 - (void)viewDidAppear而不是viewDidLoad下创建约束
这是一个老问题,但我想从文档中指出这一行:
在针对iOS 8.0或更高版本进行开发时,请将约束的isActive属性设置为true,而不是直接调用addConstraint(_ :)方法。isActive属性会自动在正确的视图中添加和删除约束。
至少,这将消除一个故障点,因为您不再需要担心在错误的视图上调用addConstraint
| 归档时间: |
|
| 查看次数: |
53077 次 |
| 最近记录: |