2014-10-25 13 views
8

Ho creato una semplice custom UICollectionViewCell nello storyboard, l'ho fatto anche programmaticamente con risultati identici.UICollectionViewCell Layout automatico

Ha un singolare UIImageView e due UILabels, in questo modo:

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

L'immagine è seduto sulla sinistra, poi a destra di essa sono due etichette sopra e sotto l'altro.

Ciascuno è distanziato con intervalli di 10 pixel e tutti centrati sull'asse Y delle celle. Le etichette quindi hanno una costante per compensarle dal centro (su e giù).

L'immagine ha un'altezza e una larghezza fisse di 44 px e le etichette hanno un'altezza fissa di 20 px (con larghezza flessibile).

I vincoli funzionano come previsto e non interrompono mai l'applicazione, ma sputano sempre queste infastidite informazioni del registro della console di seguito, in quanto apparentemente si stanno rompendo.

Se aggiungo questa linea alla sottoclasse UICollectionViewCell, i vincoli non si rompono più e sputano errori della console, ma sono completamente rotti sullo schermo.

[self.contentView setTranslatesAutoresizingMaskIntoConstraints:NO]; 

Eventuali indizi? Per favore aiuto, sembra così illogico!

Ecco il codice di errore console:

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. 

risposta

21

Ho affrontato questo problema troppo e fissato con l'aggiunta di questo all'interno della sottoclasse 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

Si potrebbe anche verificare questa risposta: UICollectionView Cell + UiLabel with AutoLayout

+1

Attualmente una rapida dovrebbe essere più simile: 'esclusione func awakeFromNib() {super.awakeFromNib() contentView.autoresizingMask = [UIViewAutoresizing.FlexibleWidth, UIViewAutoresizing.FlexibleHeight]}' – Apan

1

Subito dopo la creazione delle cellule e visualizzare il contenuto della cella sono 50x50 (non importa quanto sia grande è impostato per collezione disposizione).

Il layout automatico la prima volta funziona dopo che la cella ha ottenuto la dimensione corretta ma la vista del contenuto è ancora 50x50. Probabilmente si tratta di iOS 8. L'altro layout funziona con la dimensione della vista del contenuto corretta. Così finemente vedi un layout corretto ma l'output del log è sparpagliato.

Basta impostare in awakeFromNib qualsiasi fotogramma più grande quindi la dimensione della cella minima alla visualizzazione di contenuti per iOS 8.

Nel tuo caso

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

larghezza delle celle minimo è di 74 - somma di tutte le costanti esplicite.

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

Aveva lo stesso problema ma l'impostazione del metodo di creazione automatica non ha funzionato. E il frame di impostazione ha risolto il problema.

+0

impostando il cell.contentView.frame esplicitamente fissa il mio problema Grazie mille per aver trovato il tempo di postare. – Andy

0

Ho avuto questo problema pure. Aggiunta:

[self.contentView setTranslatesAutoresizingMaskIntoConstraints:NO]; 

ha interrotto il layout della visualizzazione. Nel mio caso, ho dovuto modificare un paio di vincoli di layout per riportare la visualizzazione nel modo desiderato. In un caso un'immagine non era centrata verticalmente perché l'altezza della cella non era sufficientemente vincolata. (Lì, ho aggiunto un vincolo di fondo all'elemento più basso nella cella). In un altro caso, un'altra immagine (in una cella diversa) non era centrata e dovevo fare un diverso cambio di vincolo. Ho aggiunto un vincolo di altezza su quell'immagine e rimosso un vincolo di cima e questo ha risolto il problema.

Ho anche avuto problemi in entrambe queste celle con UILabel s che non erano più vincolate alla larghezza della cella della vista di raccolta. Cioè, quando il testo era troppo lungo, il testo non si rimpiccioliva bene con "..." all'interno della cella, sarebbe semplicemente passato la fine della cella. In entrambi i casi, l'unico modo per aggirare il problema era di aggiungere un vincolo a larghezza fissa all'etichetta, rendendo così la larghezza dell'etichetta non adattiva. C'erano in precedenza vincoli finali che avevano funzionato prima di impostare translatesAutoresizingMaskIntoConstraints su NO.

Problemi correlati