본질적으로 보통 textLabel
레이블을 대체하는 사용자 지정 레이블을 가진 UITableViewSubclass (스타일 : UITableViewCellStyleValue1)가 있습니다. 이 값은 보통 textLabel
으로 정렬해야하지만 너비는 보통 detailTextLabel
의 왼쪽에 8 포인트 끝나야합니다. 원하는 효과를 얻지 만 셀을 사용하면 동시에 제약 조건을 충족시킬 수 없다는 예외가 발생합니다.UITableViewCell에 자동 레이아웃 제약 조건 충돌이 발생하는 이유
내가 이해할 수없는 것은 그것이 불평하는 이유와 명백한 충돌이 없다는 것입니다. 제약 조건을 설정 한 후 [self layoutIfNeeded]를 호출하면 예외가 사라집니다. 또는 후행 속성 (예 : UILayoutPriorityRequired
대신 UILayoutPriorityDefaultHigh
대신)을 구성하는 제약 조건의 우선 순위를 낮추면 자동 레이아웃 엔진에 해당 제약 조건을 무시/무시할 수 있음을 알리고 있기 때문에 예외가 사라지게됩니다.
표준 UITableView 구현이 묘사 된 설명을 처음에는 불가능하게하는 방식으로 이상하게도 textLabel
및 textLabel
을 배치한다고 가정합니다. 예를 들어 textLabel
이 실제로 셀의 오른쪽에 배치되고 detailTextLabel
이 왼쪽에 배치 된 경우 설명 된 레이아웃은 불가능합니다.
이 시나리오에서 자동 레이아웃은 언제 적용됩니까? 그것이 총을 뛰어 넘기 전에 물건을 배치하려고 시도하고 있습니까?
아니요 스토리 보드 또는 XIB가 사용되고 있습니다. 순전히 코드 기반.
#import "EBTStoreTableViewCell.h"
@interface EBTStoreTableViewCell()
@property (nonatomic, readonly, weak) UILabel *storeNameLabel;
@end
@implementation EBTStoreTableViewCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuseIdentifier];
if (self) {
self.textLabel.text = @" ";
self.detailTextLabel.text = @" ";
UILabel *storeNameLabel = [[UILabel alloc] init];
storeNameLabel.translatesAutoresizingMaskIntoConstraints = NO;
[storeNameLabel setContentCompressionResistancePriority:UILayoutPriorityDefaultLow forAxis:UILayoutConstraintAxisHorizontal];
[self.contentView addSubview:storeNameLabel];
_storeNameLabel = storeNameLabel;
[self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:storeNameLabel attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.textLabel attribute:NSLayoutAttributeLeading multiplier:1.0f constant:0.0f]];
[self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:storeNameLabel attribute:NSLayoutAttributeBaseline relatedBy:NSLayoutRelationEqual toItem:self.textLabel attribute:NSLayoutAttributeBaseline multiplier:1.0f constant:0.0f]];
[self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:storeNameLabel attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.detailTextLabel attribute:NSLayoutAttributeLeading multiplier:1.0f constant:-8.0f]];
// [self layoutIfNeeded]; // doing this makes the issue go away
}
return self;
}
// Setters ommited
@end
이 내가 얻을 예외 메시지 :
2014-04-23 11:50:42.092 Application[32507:60b] 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:0xde03910 UILabel:0xde036b0.leading == UILabel:0xde00590.leading>",
"<NSLayoutConstraint:0xde03a30 UILabel:0xde036b0.trailing == UITableViewLabel:0xde00c10.leading - 8>",
"<NSAutoresizingMaskLayoutConstraint:0xde01920 h=--& v=--& UILabel:0xde00590.midX ==>",
"<NSAutoresizingMaskLayoutConstraint:0xde1de50 h=--& v=--& H:[UILabel:0xde00590(0)]>",
"<NSAutoresizingMaskLayoutConstraint:0x17f50a10 h=--& v=--& UITableViewLabel:0xde00c10.midX ==>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0xde03a30 UILabel:0xde036b0.trailing == UITableViewLabel:0xde00c10.leading - 8>
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.
나는 같은 간단한 문제가 이러한 두통을 유발할 수 있다는 사실 때문에 나를 iOS SDK에 아프게합니다. – dreamzor