2017-06-22 10 views
3

使用可能なコンテンツに合わせて高さを自動的に変更するUITableViewCellを実装しようとしています。私は以下のレイアウトを今は持っていますが、プログラムを実行しているときはいつでも、さまざまな「Unable to constraints constraints」エラーがスローされます。制約を設定する方法に何か問題がありますか?完全のために自動サイズ変更UITableViewCell:制約を同時に満たすことができません

[LayoutConstraints] 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. 
(
    "<NSLayoutConstraint:0x60800009c2f0 UIImageView:0x7fd389002f50.height == 60 (active)>", 
    "<NSLayoutConstraint:0x60800009a8b0 UIImageView:0x7fd389002f50.top == UITableViewCellContentView:0x7fd389009b20.topMargin + 4 (active)>", 
    "<NSLayoutConstraint:0x608000097ca0 UITableViewCellContentView:0x7fd389009b20.bottomMargin >= UIImageView:0x7fd389002f50.bottom + 4 (active)>", 
    "<NSLayoutConstraint:0x600000097d40 'UIView-Encapsulated-Layout-Height' UITableViewCellContentView:0x7fd389009b20.height == 80 (active)>" 
) 

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x60800009c2f0 UIImageView:0x7fd389002f50.height == 60 (active)> 

enter image description here

enter image description here

enter image description here

、ここで私はテストのために使用している単純なコードです。

"ListViewController.m"

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 

    tableView.rowHeight = UITableViewAutomaticDimension; 
    tableView.estimatedRowHeight = 40; 
} 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return 40; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    ListTableViewCell *cell = (ListTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"ListCell" forIndexPath:indexPath]; 

    [self configureCell:cell atIndexPath:indexPath]; 
    return cell; 
} 

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { 
    // Return NO if you do not want the specified item to be editable. 
    return NO; 
} 

- (void)configureCell:(ListTableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath { 
    cell.selectionStyle = UITableViewCellSelectionStyleNone; 

    cell.hasProfile = (arc4random_uniform(10) < 3); 
    cell.hasSecondField = (arc4random_uniform(10) < 3); 
} 

ListViewCell.h

@interface ListTableViewCell : UITableViewCell { 
    IBOutlet NSLayoutConstraint *pictureWidthConstraint; 
    IBOutlet NSLayoutConstraint *pictureHeightConstraint; 
    IBOutlet NSLayoutConstraint *pictureBottomTrailingConstraint; 
    IBOutlet NSLayoutConstraint *subheaderHeightConstant; 

    IBOutlet UILabel *subheaderLabel; 
} 

@property (nonatomic, assign) BOOL hasProfile; 
@property (nonatomic, assign) BOOL hasSecondField; 

@end 

ListViewCell.m

- (void)setHasProfile:(BOOL)hasProfile { 
    _hasProfile = hasProfile; 

    if (!hasProfile) { 
     pictureHeightConstraint.constant = 0; 
     pictureWidthConstraint.constant = 0; 
    } 
    else { 
     pictureHeightConstraint.constant = 60; 
     pictureWidthConstraint.constant = 60; 
    } 
} 

- (void)setHasSecondField:(BOOL)hasSecondField { 
    _hasSecondField = hasSecondField; 

    if (!hasSecondField) { 
     subheaderLabel.text = @""; 
     subheaderHeightConstant.constant = 0; 
    } 
    else { 
     subheaderLabel.text = @"Second Label"; 
     subheaderHeightConstant.constant = 21; 
    } 
} 
+0

明らかに間違いがありますが、問題は画像ビューの高さです。あなたはimageview = 60の高さを与えていました。それがあなたに不満足な拘束の警告を与えている理由です。 imageviewの下限制約を削除し、エラーが発生するかどうかを確認します。 –

+0

autoLayoutの 'TableCell' ,,,,, https://stackoverflow.com/a/43656451/4466607 @ PF1 – Dhiru

+0

については、ここをクリックしてください。uitableviewcellのインスペクタで、行の高さに対してcheckBoxを"カスタム "に設定しようとしています。また、uitableviewcellが存在する場合、制約条件の制約80を削除する。また、私はpictureHeightConstraint.constant = 0とsubheaderHeightConstant.constant = 0を削除します – Malder

答えて

1

エラーには、画像ビューの高さ60と矛盾するいくつかの他の制約があり、すべての制約が満たされるようにこの制約が変更されています。

周囲を微調整して、その高さとどのコンストレインが衝突しているかを確認するか、自分で分解することができます。これを行うには、画像の高さの編集ボタンをクリックして、優先度を999に変更してください。 enter image description here

関連する問題