【问题标题】:Issues with programatic NSLayout constraints in tableViewCelltableViewCell 中的编程 NSLayoutconstraints 问题
【发布时间】:2016-01-26 04:45:57
【问题描述】:

我不断收到奇怪的约束错误,并且我的单元格中的视图不显示,下面是单元格类和错误日志。

我的表格视图单元格类

#import "TableViewCell.h"

@implementation TableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {

self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
    
    self.translatesAutoresizingMaskIntoConstraints = NO;
    
    self.recipeImageView = [UIImageView new];
    self.recipeImageView.image = [UIImage imageNamed:@"imagePickerBackground"];
    self.recipeImageView.layer.cornerRadius = 20;
    self.recipeImageView.layer.borderColor = [[UIColor blackColor]CGColor];
    self.recipeImageView.layer.borderWidth = 1;
    self.recipeImageView.layer.masksToBounds = YES;
    [self addSubview:self.recipeImageView];
    
    self.descriptionLabel = [UILabel new];
    self.descriptionLabel.text = @"description";
    self.descriptionLabel.font = [UIFont fontWithName:@"Chalkduster" size:12];
    self.descriptionLabel.textColor = [UIColor blackColor];
    self.descriptionLabel.layer.cornerRadius = 20;
    self.descriptionLabel.layer.borderWidth = 1;
    self.descriptionLabel.layer.masksToBounds = YES;
    self.descriptionLabel.layer.borderColor = [[UIColor blackColor]CGColor];
    [self addSubview:self.descriptionLabel];
    
    NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(_descriptionLabel, _recipeImageView);
    
    NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10-[_recipeImageView]-10-|" options:0 metrics:nil views:viewsDictionary];
    
//        NSArray *constraintsTwo = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-10-[_recipeImageView]-10-|" options:0 metrics:nil views:viewsDictionary];
    
    NSLayoutConstraint *equalConstraint = [NSLayoutConstraint constraintWithItem:self.descriptionLabel attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.recipeImageView attribute:NSLayoutAttributeWidth multiplier:1 constant:0];
    
    NSArray *verticalConstrains = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-10-[_recipeImageView(==200)]-10-[_descriptionLabel(==70)]-10-|" options:0 metrics:nil views:viewsDictionary];
    
    [self addConstraints:constraints];
//        [self addConstraints:constraintsTwo];
    [self addConstraints:verticalConstrains];
    [self addConstraint:equalConstraint];
    
}

return self;
}

@end

这里是视图控制器中的编程注册方法(表视图的数据源和委托)

在视图中确实加载了

- (void)viewDidLoad {
[super viewDidLoad];

self.tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
self.tableView.delegate = self;
self.tableView.dataSource = self;
[self registerTableView:self.tableView];
[self.view addSubview:self.tableView];

[self.tableView reloadData]; 

[self setUpNavigationBar];

}

注册方法

- (void)registerTableView:(UITableView *)tableView {

[tableView registerClass:[TableViewCell class] forCellReuseIdentifier:@"cell"];

}

索引路径处的行单元格

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

return cell; 

}

行高(设置为 300)

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

return 300;
}

错误日志(它不会崩溃,所以我不能设置断点)

2015-10-26 15:22:05.524 食谱日记[33894:1375043] 无法 同时满足约束。可能至少其中之一 以下列表中的约束是您不想要的。试试这个:(1) 查看每个约束并尝试找出您不期望的约束; (2) 找到添加了不需要的约束的代码 并修复它。 (注:如果你看到 NSAutoresizingMaskLayoutConstraints 你不明白的,参考 到 UIView 属性的文档 translatesAutoresizingMaskIntoConstraints) ( "<0x7f7f53eca790 h="--&" v="--&" uiimageview:0x7f7f53ecebd0.midx="="><0x7f7f53ecfb90 h:>

<0x7f7f53ecfb90 h:>

<0x7f7f53e077f0 h="--&" v="--&" uiimageview:0x7f7f53ecebd0.midy="="><0x7f7f53ecfcb0 v:><0x7f7f53ecfd30 v:>

<0x7f7f53ecfcb0 v:>

<0x7f7f53ecdc80 h="--&" v="--&" v:><0x7f7f53ecfd30 v:>

<0x7f7f53ecfd30 v:>

<0x7f7f53e077f0 h="--&" v="--&" uiimageview:0x7f7f53ecebd0.midy="="><0x7f7f53ecdc80 h="--&" v="--&" v:><0x7f7f53f32fb0 h="--&" v="--&" uilabel:0x7f7f53ecf310><0x7f7f53ecfd80 v:><0x7f7f53ecfdd0 v:>

<0x7f7f53ecfd80 v:>

<0x7f7f53f392e0 h="--&" v="--&" v:><0x7f7f53ecfdd0 v:>

<0x7f7f53ecfdd0 v:>

<0x7f7f53f36750 h="--&" v="--&" uiimageview:0x7f7f53c62320.midx="="><0x7f7f53e94600 h:>

<0x7f7f53e94600 h:>

<0x7f7f53f3a030 h="--&" v="--&" uiimageview:0x7f7f53c62320.midy="="><0x7f7f53e9c3a0 v:><0x7f7f53e9c420 v:>

<0x7f7f53e9c3a0 v:>

<0x7f7f53f39c30 h="--&" v="--&" v:><0x7f7f53e9c420 v:>

<0x7f7f53e9c420 v:>

<0x7f7f53f3a030 h="--&" v="--&" uiimageview:0x7f7f53c62320.midy="="><0x7f7f53f39c30 h="--&" v="--&" v:><0x7f7f53c4d870 h="--&" v="--&" uilabel:0x7f7f53ec94d0><0x7f7f53e9c470 v:><0x7f7f53e9c4c0 v:>

<0x7f7f53e9c470 v:>

<0x7f7f53c4bab0 h="--&" v="--&" v:><0x7f7f53e9c4c0 v:>

<0x7f7f53e9c4c0 v:>

<0x7f7f53c4d330 h="--&" v="--&" uiimageview:0x7f7f53ecb400.midx="="><0x7f7f53c5da10 h:>

<0x7f7f53c5da10 h:>

<0x7f7f53c4b6a0 h="--&" v="--&" uiimageview:0x7f7f53ecb400.midy="="><0x7f7f53c5de60 v:><0x7f7f53c5dee0 v:>

<0x7f7f53c5de60 v:>

<0x7f7f53c4d980 h="--&" v="--&" v:><0x7f7f53c5dee0 v:>

<0x7f7f53c5dee0 v:>

<0x7f7f53c4b6a0 h="--&" v="--&" uiimageview:0x7f7f53ecb400.midy="="><0x7f7f53c4d980 h="--&" v="--&" v:><0x7f7f53c4d660 h="--&" v="--&" uilabel:0x7f7f53f454e0><0x7f7f53c5df30 v:><0x7f7f53c5df80 v:>

<0x7f7f53c5df30 v:>

<0x7f7f53c4d6b0 h="--&" v="--&" v:><0x7f7f53c5df80 v:>

<0x7f7f53c5df80 v:>

    标签: objective-c uitableview nslayoutconstraint


    【解决方案1】:

    我想通了

    而不是

    self.translatesAutoresizingMaskIntoConstraints = NO;
    

    应该是

    self.recipeImageView.translatesAutoresizingMaskIntoConstraints = NO;
    self.descriptionLabel.translatesAutoresizingMaskIntoConstraints = NO;
    

    【讨论】:

      猜你喜欢
      • 2021-06-17
      • 2021-04-15
      • 2017-12-30
      • 2020-09-19
      • 1970-01-01
      • 1970-01-01
      • 2013-07-21
      • 2016-11-23
      • 1970-01-01
      相关资源
      最近更新 更多