2013-03-07 2 views
5

UIView의 하위 클래스에 NSLayoutConstraints을 적용하고 나는 그들이 등 모호 할 필요가 작동 방식을 이해하고 ...내가 전에 IB에 자동 레이아웃 제약 조건을 사용하여 시도했다

을하지만 당신은 전망의 몇 이상이있을 때 조금 복잡해지고 부서지기 쉽습니다.

그래서 나는 같은 문제가있는 사람에 대해 블로그를 읽고 ASCII 코드를 사용하여 대신 제약 조건을 만들었습니다.

콘텐츠 양에 따라 동적 크기를 갖는 사용자 지정 UITableViewCell이있는 UITableView가 있습니다. 자동 레이아웃을위한 완벽한 후보.

그래서 몇 가지 방법을 시도해 보았습니다. 이제는 셀에 레이블이 하나만 있도록 내용을 잘라 냈습니다.

주위에 테두리가있는 셀을 레이블로 채우려면 무엇을하고 싶습니다.

즉, 표준 크기는 셀의 각 모서리를 가리 킵니다. 나는이 같은 그것을 한 적이

...
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 
{ 
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
    if (self) { 
     _label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 10, 10)]; 
     _label.textColor = [UIColor colorWithWhite:0.53 alpha:1.0]; 
     _label.backgroundColor = [UIColor yellowColor]; 
     [self addSubview:_label]; 

     NSDictionary *views = NSDictionaryOfVariableBindings(_label); 

     [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[_label]-|" 
                    options:0 
                    metrics:nil 
                     views:views]]; 

     [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|-[_label]-|" 
                    options:0 
                    metrics:nil 
                     views:views]]; 

     self.backgroundView = [[UIView alloc] initWithFrame:CGRectZero]; 
     self.backgroundColor = [UIColor clearColor]; 
    } 
    return self; 
} 

그러나 셀이 표시 될 때 레이블은 내가 그것을 초기화하기 위해서 사용 정확히 CGRect처럼 왼쪽 상단 모서리에 내가이 부하를 얻을 수 제약에 대한 오류의 ...

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. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSAutoresizingMaskLayoutConstraint:0x1f859e40 h=--& v=--& UILabel:0x1f864a00.midX == + 5>", 
    "<NSLayoutConstraint:0x1f86a4f0 H:|-(NSSpace(20))-[UILabel:0x1f864a00] (Names: '|':MyCell:0x1f857740)>" 
) 

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x1f86a4f0 H:|-(NSSpace(20))-[UILabel:0x1f864a00] (Names: '|':MyCell:0x1f857740)> 

Break on objc_exception_throw to catch this in the debugger. 
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful. 
2013-03-07 11:56:14.841 unasys[13082:907] 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. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSLayoutConstraint:0x1f85f7b0 V:|-(NSSpace(20))-[UILabel:0x1f864a00] (Names: '|':MyCell:0x1f857740)>", 
    "<NSAutoresizingMaskLayoutConstraint:0x1f859ec0 h=--& v=--& UILabel:0x1f864a00.midY == + 5>" 
) 

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x1f85f7b0 V:|-(NSSpace(20))-[UILabel:0x1f864a00] (Names: '|':MyCell:0x1f857740)> 

Break on objc_exception_throw to catch this in the debugger. 
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful. 
2013-03-07 11:56:14.854 unasys[13082:907] 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. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSLayoutConstraint:0x1f86a540 H:[UILabel:0x1f864a00]-(NSSpace(20))-| (Names: '|':MyCell:0x1f857740)>", 
    "<NSAutoresizingMaskLayoutConstraint:0x1f859e40 h=--& v=--& UILabel:0x1f864a00.midX == + 5>", 
    "<NSAutoresizingMaskLayoutConstraint:0x1f859e80 h=--& v=--& H:[UILabel:0x1f864a00(10)]>", 
    "<NSAutoresizingMaskLayoutConstraint:0x1ed8e150 h=--& v=--& H:[MyCell:0x1f857740(320)]>" 
) 

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x1f86a540 H:[UILabel:0x1f864a00]-(NSSpace(20))-| (Names: '|':MyCell:0x1f857740)> 

Break on objc_exception_throw to catch this in the debugger. 
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful. 
2013-03-07 11:56:14.858 unasys[13082:907] 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. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSAutoresizingMaskLayoutConstraint:0x1eda7260 h=--& v=--& V:[MyCell:0x1f857740(143)]>", 
    "<NSAutoresizingMaskLayoutConstraint:0x1f859f00 h=--& v=--& V:[UILabel:0x1f864a00(10)]>", 
    "<NSLayoutConstraint:0x1f85f870 V:[UILabel:0x1f864a00]-(NSSpace(20))-| (Names: '|':MyCell:0x1f857740)>", 
    "<NSAutoresizingMaskLayoutConstraint:0x1f859ec0 h=--& v=--& UILabel:0x1f864a00.midY == + 5>" 
) 

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x1f85f870 V:[UILabel:0x1f864a00]-(NSSpace(20))-| (Names: '|':MyCell:0x1f857740)> 

그리고 나는 이것을 디버깅으로 어디에서 시작 해야할지 모릅니다. 제약 조건은 절대적으로 미미하지만 여전히 완전히 실패합니다.

누군가이 제약 조건을 작동시키는 올바른 방향으로 나를 가리킬 수 있습니까? 선 후

답변

13

_label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 10, 10)]; 

는 ambigious

을, 당신이 설정 한 제약 조건과 충돌하는의 autoresizingmask도 제약으로 설정되어 기본적으로

[_label setTranslatesAutoresizingMaskIntoConstraints:NO]; 

를 추가 문서는 여기에 있습니다 : setTranslatesAutoresizingMaskIntoConstraints

+0

굉장! 매우 감사합니다! – Fogmeister

+0

작동! 또한 자체 대신 self.contentView 물건을 추가하려면 변경해야했다 그것은 UITableViewCell 및 모든 일한 : D 감사합니다! – Fogmeister

+1

기본적으로 autolayout 오류에서 NSAutoresizingMaskLayoutConstraint'를보고 그 이유를 모른다면 어딘가에서'translatesAutoresizingMaskIntoConstraints'를 끄는 것을 잊었습니다 – iain

관련 문제