2012-12-13 25 views
7

Tôi đang phân lớp UICollectionViewCell và thực hiện tất cả bố cục trong mã với bố cục tự động. Đây là phương pháp init của tôi:Custom UICollectionViewCell Bố cục Tự động NSInternalInconsistencyException error

- (id)initWithFrame:(CGRect)frame{ 
    frame = CGRectMake(0, 0, 403, 533); 
    if (self = [super initWithFrame:frame]) { 
     self.translatesAutoresizingMaskIntoConstraints = NO; 

     PBCardPricesViewController *pricesView = [[PBCardPricesViewController alloc] init]; 
     [self addSubview:pricesView.view]; 

     UIImageView *background = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"CardBackground"]]; 
     [self addSubview:background]; 

     [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|-(20)-[background]|" options:0 metrics:nil views:@{@"background":background}]]; 
     [self addConstraint:[NSLayoutConstraint constraintWithItem:pricesView.view attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:background attribute:NSLayoutAttributeLeft multiplier:1 constant:0]]; 

     [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[background]|" options:0 metrics:nil views:@{@"background":background}]]; 
     [self addConstraint:[NSLayoutConstraint constraintWithItem:pricesView.view attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:background attribute:NSLayoutAttributeTop multiplier:1 constant:0]]; 
    } 

    return self; 
} 

Khi tôi nhận xét ra dòng translateAutoresizingMask tôi nhận được:

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:0x1d83f540 H:[UIImageView:0x1d83f950]-(0)-| (Names: '|':PBCardViewCollectionCell:0x1d83b970)>", 

    "<NSAutoresizingMaskLayoutConstraint:0x1c55ad20 h=--& v=--& H:[PBCardViewCollectionCell:0x1d83b970(393)]>", 

    "<NSAutoresizingMaskLayoutConstraint:0x1c559410 h=--& v=--& UIImageView:0x1d83f950.midX == + 191.5>", 

    "<NSAutoresizingMaskLayoutConstraint:0x1c559450 h=--& v=--& H:[UIImageView:0x1d83f950(383)]>" 

) 



Will attempt to recover by breaking constraint 

<NSLayoutConstraint:0x1d83f540 H:[UIImageView:0x1d83f950]-(0)-| (Names: '|':PBCardViewCollectionCell:0x1d83b970)> 



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. 

Khi tôi tôi không nhận được lỗi này: Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Auto Layout still required after executing -layoutSubviews. UICollectionView's implementation of -layoutSubviews needs to call super.' Làm thế nào để có được điều này để hiển thị cách tôi muốn nó? Tôi đang thiếu gì?

+0

Là "@" | - (20) - [nền] | "" phải có chữ H: ở phía trước hoặc đây có phải là kiểu cú pháp tôi không biết? Ngoài ra, có vẻ như "pricesView" sẽ được phát hành sau hàm init ... do đó bạn chỉ có chế độ xem. Điều đó có gây ra vấn đề không? – yuf

+0

H: là tùy chọn. Nếu không có H hoặc V nào được chỉ định, nó sẽ mặc định là H. Cho thời điểm mất bộ điều khiển xem là tốt. Tôi chỉ cố gắng để có được bố trí xuống. – Civatrix

+0

Gotcha, không biết về H, hay. Bạn đã thử sửa các lỗi ràng buộc mà không xóa mặt nạ thay đổi kích thước tự động? Tôi nghĩ rằng mặt nạ được yêu cầu trong các tế bào ... Thay vào đó, làm backgroundView.translatesAutoresizingMaskIntoConstraints = NO; và pricesView.view.translatesAutoresizingMaskIntoConstraints = NO; – yuf

Trả lời

14

Gửi bài bình luận của tôi là câu trả lời:

Trong kinh nghiệm của tôi, collectionViewCells (và tableViewCells) yêu cầu autoresizingmask của họ hoặc họ ném ngoại lệ mà bạn nhìn thấy. Nhưng bạn đang nhận được các cuộc xung đột hạn chế do sự subviews bạn thêm, vì vậy chỉ cần loại bỏ các mặt nạ từ subviews của nó:

backgroundView.translatesAutoresizingMaskIntoConstraints = NO; 
pricesView.view.translatesAutoresizingMaskIntoConstraints = NO; // you might get it to work without doing this line 

Tôi cũng cố gắng nhớ để loại bỏ các mặt nạ trên quan điểm tôi tạo sử dụng alloc (tức là không phải từ một xib), vì hầu hết thời gian họ đưa ra xung đột.

+0

Cảm ơn bạn một lần nữa, rất nhiều thứ để học với bố cục tự động mới. – Civatrix

+7

Tôi thực sự phải thiết lập [trên 'contentView'] (https://github.com/artsy/eidolon/pull/123/files#diff-d8eaed9deb2eb4f390a6c8a637465fdbR33). –

+0

Thiết lập điều này trên contentView là cần thiết cho tôi, sau đó thêm các ràng buộc chiều rộng và chiều cao – Philip