Relación de aspecto personalizada con Diseño automático
Digamos que tengo una UIView que contiene una UIView hija que tiene que ser 4:3. Quiero resolver este problema usando el diseño automático en el código.
He estado luchando con el diseño automático, pero todavía no he descubierto cómo hacer esto.
¿sabes cómo resolver este problema?
Muchas Gracias.
Adjunto una imagen para explicar mejor lo que quiero decir. http://d.pr/i/d0Oc
38
3 answers
Me di cuenta.
//Given a childView...
NSLayoutConstraint *constraint =[NSLayoutConstraint
constraintWithItem:childView
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:childView
attribute:NSLayoutAttributeHeight
multiplier:4.0/3.0 //Aspect ratio: 4*height = 3*width
constant:0.0f];
[childView addConstraint:constraint];
95
Author: JaviAlgaba,
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
2015-08-21 06:47:59
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
2015-08-21 06:47:59
Aquí está la sintaxis para Swift 3:
NSLayoutConstraint(
item: item,
attribute: .width,
relatedBy: .equal,
toItem: item,
attribute: .height,
multiplier: width / height,
constant: 0)
1
Author: user1615898,
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-15 08:11:26
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-15 08:11:26
He intentado todas las respuestas anteriores, pero no funcionó, y esta es mi solución con swift 3:
let newConstraint = NSLayoutConstraint(item: yourView, attribute: .width, relatedBy: .equal, toItem: yourView, attribute: .height, multiplier: 560.0/315.0, constant: 0)
yourView.addConstraint(newConstraint)
NSLayoutConstraint.activate([newConstraint])
NSLayoutConstraint.deactivate(yourView.constraints)
yourView.layoutIfNeeded()
0
Author: tuandapen,
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-09-25 09:12:30
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-09-25 09:12:30