Aplicación de NSLayoutConstraints a una subclass UIView

Intenté usar las limitaciones de AutoLayout en IB antes y entiendo cómo funcionan y que necesitan ser inequívocas, etc.

Pero cuando tienes más de un par de vistas, se vuelve un poco complicado y tiende a romperse.

Entonces, acabo de leer un blog sobre un chico que tiene el mismo problema y el uso de código ASCII para crear las restricciones.

Tengo una UITableView con una ITUableViewCell personalizada que tiene un tamaño dynamic basado en la cantidad de contenido. Un candidato perfecto para el layout automático.

Entonces, he intentado varias forms de hacerlo y ahora he networkingucido el contenido para que solo haya una sola label en la celda.

Lo que me gustaría si la label llenara la celda con un borde alnetworkingedor.

es decir, el tamaño estándar apunta desde cada borde de la celda.

Lo he hecho así …

- (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; } 

Pero cuando se muestra la celda, la label se encuentra en la esquina superior izquierda exactamente igual que la CGRect que solía iniciar y obtengo esta carga de errores sobre las restricciones …

 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 )> 

y no tengo idea de dónde comenzar con la debugging de esto. Las restricciones son absolutamente mínimas pero todavía fallan por completo.

¿Alguien puede señalarme en la dirección correcta de cómo hacer que esta restricción funcione?

Despues de la linea

 _label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 10, 10)]; 

añadir

 [_label setTranslatesAutoresizingMaskIntoConstraints:NO]; 

Por defecto, la máscara de autoría también se convierte en restricciones, lo que entra en conflicto con las restricciones que ha establecido, lo que la convierte en ambigua.

Los documentos están aquí: setTranslatesAutoresizingMaskIntoConstraints