2014-10-25 8 views
8

Zrobiłem prosty niestandardowy UICollectionViewCell w moim storybooku, zrobiłem to również programowo z identycznymi wynikami.UICollectionViewCell Auto Layout

Posiada pojedynczą UIImageView i dwa UILabels, tak:

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

Obraz jest siedzącego na lewo, potem w prawo na to są dwie etykiety nad i pod sobą.

Każda z nich jest oddzielona 10-pikselowymi przerwami, a wszystkie są wyśrodkowane na osi Y komórek. Etykiety mają stałą wartość, która kompensuje je od środka (góra i dół).

Obraz ma stałą wysokość i szerokość 44 pikseli, a etykiety mają stałą wysokość 20 pikseli (o elastycznej szerokości).

Ograniczenia działają zgodnie z oczekiwaniami i nigdy nie łamią aplikacji, ale zawsze wypluwają te denerwujące informacje o dzienniku konsoli poniżej, ponieważ najwyraźniej się zrywają.

Jeśli dodaję ten wiersz do mojej podklasy UICollectionViewCell, ograniczenia nie będą już łamać i wypluwają błędy konsoli, ale są całkowicie zepsute na ekranie.

[self.contentView setTranslatesAutoresizingMaskIntoConstraints:NO]; 

Jakieś wskazówki? Proszę, pomóż, wydaje się to takie nielogiczne!

Oto kod błędu konsola:

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. 

Odpowiedz

21

I wobec tego problemu zbyt i naprawić go, dodając to wewnątrz podklasy 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

Można również sprawdzić tę odpowiedź: UICollectionView Cell + UiLabel with AutoLayout

+1

Obecnie Swift powinna być większa, takie jak: korekcja 'func awakeFromNib()() {super.awakeFromNib contentView.autoresizingMask = [UIViewAutoresizing.FlexibleWidth, UIViewAutoresizing.FlexibleHeight]}' – Apan

1

Zaraz po komórka tworzenie i przeglądanie zawartości komórki są 50x50 (nie ma znaczenia jaki rozmiar jest ustawiony przez kolekcji układ).

Układ automatyczny po raz pierwszy działa po uzyskaniu prawidłowego rozmiaru komórki, ale widok treści nadal wynosi 50x50. Prawdopodobnie jest to problem z systemem iOS 8. Inny układ działa z prawidłowym rozmiarem widoku treści. Tak więc widzisz prawidłowy układ, ale dane wyjściowe dziennika są zaśmiecone.

Wystarczy ustawić w awakeFromNib każdy rama większy niż minimalny rozmiar komórki do zawartości widoku dla iOS 8.

W twoim przypadku

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

minimalna szerokość komórki jest 74 - suma wszystkich jawnych stałych.

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

Miał ten sam problem, ale ustawienie funkcji autouzupełniania nie działało. I ustawiona ramka rozwiązała problem.

+0

ustawiania cell.contentView.frame wyraźnie stałe mój problem. Dziękuję bardzo za poświęcenie czasu na publikację. – Andy

0

Miałem również ten problem. Dodawanie:

[self.contentView setTranslatesAutoresizingMaskIntoConstraints:NO]; 

przerwał układ mojego widoku. W moim przypadku musiałem zmodyfikować kilka ograniczeń układu, aby uzyskać widok tak, jak chciałem. W jednym przypadku obraz nie był wyśrodkowany w pionie, ponieważ wysokość komórki była niewystarczająco ograniczona. (Tam dodano dolne ograniczenie do najniższego elementu w komórce). W innym przypadku inny obraz (w innej komórce) nie był wyśrodkowany i musiałem dokonać innej zmiany ograniczeń. Dodałem ograniczenie wysokości tego obrazu i usunięto górne ograniczenie, które rozwiązało problem.

Też miałem problemy w obu tych komórkach z UILabel s, które nie były już ograniczone do szerokości komórki widoku kolekcji. Oznacza to, że gdy tekst był zbyt długi, tekst nie ładnie przebiegałby z "..." w komórce, po prostu mijałby koniec komórki. W obu tych przypadkach jedynym rozwiązaniem, które mogłem wymyślić, było dodanie do etykiety ograniczenia o stałej szerokości, co spowodowałoby, że szerokość etykiety nie była adaptacyjna. Poprzednio występowały ograniczenia, które działały przed ustawieniem translatesAutoresizingMaskIntoConstraints na NIE.