无法满足iOS 9中以编程方式完成的基本约束

时间:2016-10-14 06:08:29

标签: ios objective-c autolayout ios-autolayout

我一直在努力尝试满足一些基本限制,但没有运气。我以前使用过约束,而应该似乎有效。我不确定这是否是一个Apple问题,因为我遇到了其他问题,但我所遵循的解决方案并没有解决问题。

这是我正在运行的代码:

UITableViewController viewDidLoad:

self.tableView.rowHeight = UITableViewAutomaticDimension;
self.tableView.estimatedRowHeight = 100;

TestTableViewCell init:

-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {

    self.contentView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

    self.mainTextLabel = [[UILabel alloc] init];
    self.mainTextLabel.frame = CGRectMake(0, 0, 0, 0);
    [self.mainTextLabel setTextColor:[UIColor blackColor]];
    [self.mainTextLabel setBackgroundColor:[UIColor colorWithHue:32 saturation:100 brightness:63 alpha:1]];
    [self.mainTextLabel setFont:[UIFont fontWithName:@"HelveticaNeue" size:18.0f]];
    self.mainTextLabel.numberOfLines = 0;
    [self.contentView addSubview:self.mainTextLabel];

    [self setupConstraints];
}
return self;

TestTableViewCell设置约束:

- (void)setupConstraints {

NSDictionary *viewsDictionary = @{@"textLabel":self.mainTextLabel};

NSArray *mainHorizontalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-20-[textLabel]-20-|"
                                                                             options:0
                                                                             metrics:nil
                                                                               views:viewsDictionary];
[self.contentView addConstraints:mainHorizontalConstraints];


NSArray *mainVerticalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-20-[textLabel]-20-|"
                                                                           options:0
                                                                           metrics:nil
                                                                             views:viewsDictionary];
[self.contentView addConstraints:mainVerticalConstraints];

收到错误讯息:

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) 

我只是设置基本的顶部,底部,前导和尾随约束,并且约束正在破坏。我错过了什么吗?

1 个答案:

答案 0 :(得分:0)

在mainTextLabel上将translatesAutoresizingMaskIntoConstraints设置为NO可以解决问题。