Ho provato con i vincoli layout automatico in IB prima e ho capito come funzionano e che hanno bisogno di essere inequivocabile ecc ...Applicando NSLayoutConstraints ad una sottoclasse UIView
Ma quando si dispone di più di un paio di punti di vista diventa un po 'complicato e tende a rompersi.
Quindi, ho appena letto un blog su un ragazzo che ha lo stesso problema e usa il codice ASCII per creare i vincoli.
Ho un UITableView con UITableViewCell personalizzato che ha una dimensione dinamica basata sulla quantità di contenuto. Un candidato perfetto per il layout automatico.
Quindi, ho provato diversi modi di farlo e ora ho ridotto il contenuto in modo che ci sia solo una singola etichetta nella cella.
Cosa mi piacerebbe se l'etichetta riempia la cella con un bordo attorno.
, ad esempio, la dimensione standard punta da ciascun bordo della cella.
ho fatto così ...
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
_label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
_label.textColor = [UIColor colorWithWhite:0.53 alpha:1.0];
_label.backgroundColor = [UIColor yellowColor];
[self addSubview:_label];
NSDictionary *views = NSDictionaryOfVariableBindings(_label);
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[_label]-|"
options:0
metrics:nil
views:views]];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|-[_label]-|"
options:0
metrics:nil
views:views]];
self.backgroundView = [[UIView alloc] initWithFrame:CGRectZero];
self.backgroundColor = [UIColor clearColor];
}
return self;
}
Ma quando viene visualizzata la cellula l'etichetta si trova nell'angolo in alto a sinistra esattamente come il CGRect ho usato per init e ottengo questo carico di errori sui vincoli ...
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)
(
"<NSAutoresizingMaskLayoutConstraint:0x1f859e40 h=--& v=--& UILabel:0x1f864a00.midX == + 5>",
"<NSLayoutConstraint:0x1f86a4f0 H:|-(NSSpace(20))-[UILabel:0x1f864a00] (Names: '|':MyCell:0x1f857740)>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x1f86a4f0 H:|-(NSSpace(20))-[UILabel:0x1f864a00] (Names: '|':MyCell:0x1f857740)>
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.
2013-03-07 11:56:14.841 unasys[13082:907] 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:0x1f85f7b0 V:|-(NSSpace(20))-[UILabel:0x1f864a00] (Names: '|':MyCell:0x1f857740)>",
"<NSAutoresizingMaskLayoutConstraint:0x1f859ec0 h=--& v=--& UILabel:0x1f864a00.midY == + 5>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x1f85f7b0 V:|-(NSSpace(20))-[UILabel:0x1f864a00] (Names: '|':MyCell:0x1f857740)>
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.
2013-03-07 11:56:14.854 unasys[13082:907] 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:0x1f86a540 H:[UILabel:0x1f864a00]-(NSSpace(20))-| (Names: '|':MyCell:0x1f857740)>",
"<NSAutoresizingMaskLayoutConstraint:0x1f859e40 h=--& v=--& UILabel:0x1f864a00.midX == + 5>",
"<NSAutoresizingMaskLayoutConstraint:0x1f859e80 h=--& v=--& H:[UILabel:0x1f864a00(10)]>",
"<NSAutoresizingMaskLayoutConstraint:0x1ed8e150 h=--& v=--& H:[MyCell:0x1f857740(320)]>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x1f86a540 H:[UILabel:0x1f864a00]-(NSSpace(20))-| (Names: '|':MyCell:0x1f857740)>
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.
2013-03-07 11:56:14.858 unasys[13082:907] 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)
(
"<NSAutoresizingMaskLayoutConstraint:0x1eda7260 h=--& v=--& V:[MyCell:0x1f857740(143)]>",
"<NSAutoresizingMaskLayoutConstraint:0x1f859f00 h=--& v=--& V:[UILabel:0x1f864a00(10)]>",
"<NSLayoutConstraint:0x1f85f870 V:[UILabel:0x1f864a00]-(NSSpace(20))-| (Names: '|':MyCell:0x1f857740)>",
"<NSAutoresizingMaskLayoutConstraint:0x1f859ec0 h=--& v=--& UILabel:0x1f864a00.midY == + 5>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x1f85f870 V:[UILabel:0x1f864a00]-(NSSpace(20))-| (Names: '|':MyCell:0x1f857740)>
e non ho idea di dove iniziare con il debug di questo. I vincoli sono assolutamente minimi, ma continua a fallire completamente.
Qualcuno può indicarmi la giusta direzione su come far funzionare questo vincolo?
Impressionante! Grazie mille! – Fogmeister
Funziona! Ho anche dovuto cambiare per aggiungere roba a self.contentView invece di self in quanto è un UITableViewCell e poi tutto ha funzionato: D Grazie! – Fogmeister
Fondamentalmente se vedi 'NSAutoresizingMaskLayoutConstraint' in un errore di auto-caricamento e non sai perché è lì, hai dimenticato di disattivare' translatesAutoresizingMaskIntoConstraints' da qualche parte – iain