자동 레이아웃에서 하위 뷰의 X를 중앙에 배치하면 "제약 조건에 대해 준비되지 않음"이 발생합니다.
펜촉을 통해 초기화되는 사용자 지정 UIView 하위 클래스가 있습니다.
에서가 -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]];
이로 인해 중단되고 다음과 같은 출력이 발생합니다.
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]
아래 만 고려할 경우 뷰 계층 구조에 없기 때문입니다 .
자세한 내용 은 방법 에 대한 문서 의 토론 섹션을 참조하십시오 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]];
여기에 오는 다른 사람들을 위해 : 계층에 하위 뷰를 추가하기 전에 제약 조건을 추가했기 때문에이 오류가 발생했습니다.
짧은 답변 : 스왑 부모와 자식보기. 상위 뷰
라고 가정 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]))
Obj-C
[subview setTranslatesAutoresizingMaskIntoConstraints:NO];
[self addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat:@"H:|-0-[subview]-0-|"
options:NSLayoutFormatDirectionLeadingToTrailing
metrics:nil
views:NSDictionaryOfVariableBindings(subview)]];
추가 팁 :
iOS7에서 잘 실행되는 새로 개발 된 앱이 있지만 iOS8에서 "The view hierarchy is not ready for the constraint"라는 오류가 발생합니다.
다른 게시물을 읽으면 제약 조건을 만들기 전에 (하위)보기를 추가해야한다고 말했습니다. 나는 그것을했지만 여전히 오류가 있습니다.
Then I figured out that I should create constraints under -(void)viewDidAppear instead of viewDidLoad
This is an old question but I want to point out this line from the docs:
When developing for iOS 8.0 or later, set the constraint’s isActive property to true instead of calling the addConstraint(_:) method directly. The isActive property automatically adds and removes the constraint from the correct view.
At the very least, this will remove one point of failure since you no longer need to worry about calling addConstraint on the wrong view
In my case, it was solved by using root view as a container of constraints instead of a currently created view.
I mean, use inside viewController
view.addConstraints([leftConstraintImage, topConstraintImage])
instead of imageView.addConstraints...
As @CharlesA points out in his answer the problem is that the view you are adding is not in the proper view hierarchy. His answer is a good one, but I'm going to throw in some detail for a particular use-case that caused this problem for me:
I had this happen when attempting to add constraints to a view whose view controller I had instantiated using instantiateViewControllerWithIdentifier
. To solve the problem, I used [childViewController didMoveToParentViewController:self]
. This added the view into the view hierarchy that was referenced by my constraint.
First add your subView and then add your constraints like this
addSubview(yourSubViewHere)
yourSubViewHere.translatesAutoresizingMaskIntoConstraints = false
addConstraint(NSLayoutConstraint(....
'Program Tip' 카테고리의 다른 글
SQlite : 선택 하시겠습니까? (0) | 2020.10.08 |
---|---|
날짜가 주말인지 확인 PHP (0) | 2020.10.08 |
UIImage의 불투명도 / 알파를 설정하는 방법은 무엇입니까? (0) | 2020.10.08 |
Intellij에서 @author 자동 완성 (0) | 2020.10.08 |
Axios는 URL이 작동하지만 두 번째 매개 변수를 개체로 사용하면 작동하지 않습니다. (0) | 2020.10.08 |