2014-10-25 31 views
8

Tôi đã tạo thành một tùy chỉnh đơn giản UICollectionViewCell trong bảng phân cảnh của mình, tôi cũng đã làm điều này một cách có lập trình với kết quả giống hệt nhau.Bố cục tự động UICollectionViewCell

Nó có một số ít UIImageView và hai UILabels, như vậy:

  <--UIIMAGEVIEW-->   <--UILABEL--------> 
|-(10px)- |    | -(10px)-      -(10px)-| 
      <--------------->   <--UILABEL--------> 

Hình ảnh ngồi bên trái, sau đó sang bên phải của nó là hai nhãn trên và dưới mỗi khác.

Mỗi khoảng cách đều nhau với khoảng cách 10 pixel và tất cả được căn giữa trên trục Y của ô. Các nhãn sau đó có một hằng số để bù đắp chúng từ trung tâm (lên và xuống).

Hình ảnh có chiều cao và chiều rộng cố định là 44px và các nhãn có chiều cao cố định là 20px (với chiều rộng linh hoạt).

Các ràng buộc hoạt động như mong đợi và không bao giờ phá vỡ ứng dụng, nhưng chúng luôn nhổ ra thông tin nhật ký bảng điều khiển gây phiền nhiễu này bên dưới, vì chúng rõ ràng đang bị hỏng.

Nếu tôi thêm dòng này vào lớp con UICollectionViewCell của mình, các ràng buộc không còn phá vỡ và loại bỏ lỗi giao diện điều khiển, nhưng chúng hoàn toàn bị hỏng trên màn hình.

[self.contentView setTranslatesAutoresizingMaskIntoConstraints:NO]; 

Bất kỳ manh mối nào? Xin hãy giúp, nó có vẻ vô lý!

Đây là mã giao diện điều khiển lỗi:

2014-10-25 18:28:14.273 Air Plan[1579:580797] 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:0x17009edc0 H:[UIImageView:0x1701f7d00(44)]>", 
    "<NSLayoutConstraint:0x17009f130 H:|-(10)-[UIImageView:0x1701f7d00] (Names: '|':UIView:0x170183810)>", 
    "<NSLayoutConstraint:0x17009f270 H:[UIImageView:0x1701f7d00]-(10)-[UILabel:0x155e59970'detailLbl']>", 
    "<NSLayoutConstraint:0x17009f310 H:[UILabel:0x155e59970'detailLbl']-(10)-| (Names: '|':UIView:0x170183810)>", 
    "<NSAutoresizingMaskLayoutConstraint:0x17409eff0 h=--& v=--& H:[UIView:0x170183810(50)]>" 
) 

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x17009edc0 H:[UIImageView:0x1701f7d00(44)]> 

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. 
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful. 
2014-10-25 18:28:14.276 Air Plan[1579:580797] 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:0x17009fcc0 H:[UIImageView:0x1701fa800(44)]>", 
    "<NSLayoutConstraint:0x17009ff40 H:|-(10)-[UIImageView:0x1701fa800] (Names: '|':UIView:0x170180a90)>", 
    "<NSLayoutConstraint:0x1702800a0 H:[UIImageView:0x1701fa800]-(10)-[UILabel:0x155e5b440'detailLbl']>", 
    "<NSLayoutConstraint:0x170280140 H:[UILabel:0x155e5b440'detailLbl']-(10)-| (Names: '|':UIView:0x170180a90)>", 
    "<NSAutoresizingMaskLayoutConstraint:0x17409ea00 h=--& v=--& H:[UIView:0x170180a90(50)]>" 
) 

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x17009fcc0 H:[UIImageView:0x1701fa800(44)]> 

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. 
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful. 
2014-10-25 18:28:14.278 Air Plan[1579:580797] 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:0x170280a00 H:[UIImageView:0x1701fab00(44)]>", 
    "<NSLayoutConstraint:0x170280c80 H:|-(10)-[UIImageView:0x1701fab00] (Names: '|':UIView:0x1701822f0)>", 
    "<NSLayoutConstraint:0x170280dc0 H:[UIImageView:0x1701fab00]-(10)-[UILabel:0x155e5cd10'detailLbl']>", 
    "<NSLayoutConstraint:0x170280e60 H:[UILabel:0x155e5cd10'detailLbl']-(10)-| (Names: '|':UIView:0x1701822f0)>", 
    "<NSAutoresizingMaskLayoutConstraint:0x17409f180 h=--& v=--& H:[UIView:0x1701822f0(50)]>" 
) 

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x170280a00 H:[UIImageView:0x1701fab00(44)]> 

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. 
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful. 
2014-10-25 18:28:14.280 Air Plan[1579:580797] 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:0x170281090 H:[UIImageView:0x1701fae00(44)]>", 
    "<NSLayoutConstraint:0x170281310 H:|-(10)-[UIImageView:0x1701fae00] (Names: '|':UIView:0x170182220)>", 
    "<NSLayoutConstraint:0x1702813b0 H:[UIImageView:0x1701fae00]-(10)-[UILabel:0x155e5d620'titleLbl']>", 
    "<NSLayoutConstraint:0x170281400 H:[UILabel:0x155e5d620'titleLbl']-(10)-| (Names: '|':UIView:0x170182220)>", 
    "<NSAutoresizingMaskLayoutConstraint:0x17409fd60 h=--& v=--& H:[UIView:0x170182220(50)]>" 
) 

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x170281090 H:[UIImageView:0x1701fae00(44)]> 

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. 
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful. 
2014-10-25 18:28:14.282 Air Plan[1579:580797] 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:0x170281b80 H:[UIImageView:0x1701fb100(44)]>", 
    "<NSLayoutConstraint:0x17409f4a0 H:|-(10)-[UIImageView:0x1701fb100] (Names: '|':UIView:0x170182150)>", 
    "<NSLayoutConstraint:0x17409f5e0 H:[UIImageView:0x1701fb100]-(10)-[UILabel:0x155d64210'detailLbl']>", 
    "<NSLayoutConstraint:0x17409f680 H:[UILabel:0x155d64210'detailLbl']-(10)-| (Names: '|':UIView:0x170182150)>", 
    "<NSAutoresizingMaskLayoutConstraint:0x1742800f0 h=--& v=--& H:[UIView:0x170182150(50)]>" 
) 

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x170281b80 H:[UIImageView:0x1701fb100(44)]> 

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. 
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful. 

Trả lời

21

tôi phải đối mặt với vấn đề mà quá và cố định nó bằng cách thêm này bên trong lớp con UICollectionViewCell:

SWIFT 3

override func awakeFromNib() { 
    contentView.autoresizingMask = [UIViewAutoresizing.flexibleWidth, UIViewAutoresizing.flexibleHeight] 
} 

OBJC

- (void)awakeFromNib{ 
    self.contentView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; 
} 

Xcode 6.1 iOS 8.0.2, 8.1

Bạn cũng có thể kiểm tra câu trả lời này: UICollectionView Cell + UiLabel with AutoLayout

+1

Hiện nay để nhanh chóng nên được như thế hơn: 'override func awakeFromNib() {super.awakeFromNib() contentView.autoresizingMask = [UIViewAutoresizing.FlexibleWidth, UIViewAutoresizing.FlexibleHeight]} ' – Apan

1

Ngay sau khi tế bào tạo ra và xem nội dung di động của đang 50x50 (không có vấn đề gì kích thước được thiết lập bởi bộ sưu tập bố trí).

Bố cục tự động lần đầu tiên hoạt động sau khi ô có kích thước chính xác nhưng chế độ xem nội dung vẫn là 50x50. Có lẽ đó là vấn đề iOS 8. Bố cục khác hoạt động với kích thước chế độ xem nội dung chính xác. Vì vậy, bạn thấy một cách đúng đắn bố trí chính xác nhưng đầu ra đăng nhập là rải rác.

Chỉ cần thiết lập trong awakeFromNib bất kỳ khung lớn hơn sau đó kích thước tế bào tối thiểu để xem nội dung dành cho iOS 8.

Trong trường hợp của bạn

|-(10px)-UIImageView(44px)-(10px)-UILabel-(10px)-| 

tối thiểu chiều rộng di động là 74 - tổng của tất cả các hằng số rõ ràng.

- (void)awakeFromNib { 
    [super awakeFromNib]; 
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.) { 
     self.contentView.frame = CGRectMake(0, 0, 200, 100); 
    } 
} 

Đã xảy ra sự cố tương tự nhưng thiết lập auoresizingMask không hoạt động. Và khung thiết lập đã giải quyết được sự cố.

+0

thiết lập một cách rõ ràng cell.contentView.frame cố định vấn đề của tôi. Cảm ơn bạn rất nhiều vì đã dành thời gian để đăng bài. – Andy

0

Tôi cũng gặp vấn đề này. Đang thêm:

[self.contentView setTranslatesAutoresizingMaskIntoConstraints:NO]; 

đã phá vỡ bố cục của chế độ xem của tôi. Trong trường hợp của tôi, tôi đã phải tinh chỉnh một vài ràng buộc bố trí để có được cái nhìn trở lại theo cách tôi muốn. Trong một trường hợp, hình ảnh không được căn giữa theo chiều dọc bởi vì chiều cao của ô không đủ giới hạn. (Ở đó, tôi đã thêm một ràng buộc đáy vào phần tử thấp nhất trong ô). Trong trường hợp khác, một hình ảnh khác (trong một ô khác) không tập trung và tôi phải thực hiện một sự thay đổi ràng buộc khác. Tôi đã thêm một ràng buộc chiều cao trên hình ảnh đó, và loại bỏ ràng buộc hàng đầu và giải quyết vấn đề.

Tôi cũng gặp sự cố trong cả hai ô này với UILabel s không còn bị giới hạn với chiều rộng của ô xem bộ sưu tập. Nghĩa là, khi văn bản quá dài, văn bản sẽ không bị tắt đi một cách độc đáo với "..." bên trong ô, nó sẽ chạy qua cuối ô. Trong cả hai trường hợp này, công việc duy nhất xung quanh tôi có thể đưa ra là thêm một ràng buộc cố định chiều rộng cho nhãn, do đó làm cho chiều rộng của nhãn không thích ứng. Trước đây đã có các ràng buộc theo sau đã hoạt động trước khi thiết lập translatesAutoresizingMaskIntoConstraints thành NO.

Các vấn đề liên quan