iOS Nslayoutconstrain Ancho fijo usando constraintWithItem
Me gustaría establecer una restricción para dar a un UIButton un ancho fijo (constante) mediante programación. Sé que puedo hacer esto con constraintsWithVisualFormat, pero he estado usando constraintWithItem para todas mis restricciones en código. Me preguntaba por curiosidad / coherencia si había alguna manera de hacer esto con constraintWithItem.
5 answers
Encontré mi solución. Simplemente establezca el otro objeto en nil, y el otro atributo en NSLayoutAttributeNotAnAttribute (esto fue lo que fallé en pensar) y use el parámetro constante para el ancho fijo:
[self addConstraint:[NSLayoutConstraint constraintWithItem:myButton
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1.0
constant:200]];
Editar: dado que esta respuesta todavía parece obtener una buena parte de las opiniones, pensé en agregar la sintaxis Swift:
self.addConstraint(NSLayoutConstraint(
item: myButton,
attribute: .width,
relatedBy: .equal,
toItem: nil,
attribute: .notAnAttribute,
multiplier: 1.0,
constant: 200))
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-03-24 16:08:48
¿Qué tal usar Anclajes de diseño?
myView.widthAnchor.constraintEqualToConstant(29).active = true
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-08-23 13:04:11
En swift:
let width = 120
let constraint = NSLayoutConstraint(
item: myView,
attribute: .width,
relatedBy: .equal,
toItem: nil,
attribute: .notAnAttribute,
multiplier: 1.0,
constant: width)
NSLayoutConstraint.activateConstraints([constraint])
Entonces puede cambiar el valor constante de la restricción
constraint.constant = width * 2
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-05-14 16:10:07
Aquí hay un código simple para el botón con ancho fijo.
Formato visual: -
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H: [myButton(==50)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(myButton)]];
Utilice este código para la restricción usando el formato visual donde uno mismo.view es la vista superior de tu botón y myButton es el nombre de tu botón y 50 es el ancho de myButton. Puede cambiar estos valores de acuerdo con obtener la restricción deseada.
Formato ConstraintWithItem: -
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:myButton attribute: NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute: NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:50.0]];
Use este código para restricción usando el formato constraintWithItem donde self.la vista es la vista superior de tu botón y myButton es el nombre de tu botón y 50 es el ancho de myButton. Puede cambiar estos valores de acuerdo con obtener la restricción deseada.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-12-29 05:26:53
En lugar de buscar una altura explícita (28), una mejor idea sería buscar una restricción height
{
loginButton.constraints.first(where: { $0.firstAttribute == .height })?.constant = 40
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-12-20 18:12:22