Opacidad del color de fondo de Swift UIView
Tengo un UIView
con un UILabel
en él. Quiero que la UIView tenga un color de fondo blanco, pero con una opacidad del 50%. El problema con la configuración view.alpha = 0.5
es que la etiqueta también tendrá una opacidad del 50%, así que descubrí que tal vez sería posible tener un UIView
con color de fondo blanco y opacidad (white_view), y luego tener otro UIView
con la etiqueta (label_view). Luego agregue el "white_view" a "label_view" haciendo esto: label_view.addSubview(white_view)
. Esto aparentemente no funciona. Me gustaría hacer como: label_view.backgroundView(white_view)
pero no puedes establecer una vista de fondo en un UIView
como puedes hacer en un UICollectionView
por ejemplo.
¿Alguien tiene alguna idea de cómo resolver esto?
EDITAR Debido a que varias respuestas son aproximadamente iguales, lo escribiré aquí. Ahora he intentado incluso estos:
label_view1.backgroundColor = UIColor.whiteColor().colorWithAlphaComponent(0.5)
label_view1.addSubview(firstPlacelbl)
endGameView.addSubview(label_view1)
Y
label_view1.backgroundColor = UIColor(white: 1, alpha: 0.5)
label_view1.addSubview(firstPlacelbl)
endGameView.addSubview(label_view1)
Y aún así la etiqueta también se ve afectada por el alfa, y obtiene una opacidad del 50%. No entiendo lo que hago mal porque solo establezco el alfa de colores a 0.5 y no el etiqueta. Alguna idea?
6 answers
Puede establecer el color de fondo de la vista en el UIColor con alfa, y no afectar a la vista.alfa
view?.backgroundColor = UIColor(white: 1, alpha: 0.5)
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-12 08:11:08
Establecer la propiedad alpha
de una vista afecta a sus subviews. Si solo desea un fondo transparente, establezca la proprty backgroundColor
de la vista en un color que tenga un componente alfa menor que 1.
view.backgroundColor = UIColor.whiteColor().colorWithAlphaComponent(0.5)
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-12 08:23:39
En Swift 3.0
yourView.backgroundColor = UIColor.black.withAlphaComponent(0.5)
Esto funciona para mí en xcode 8.2.
Puede que te ayude.
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-06-06 11:21:09
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-13 15:14:15
El problema que ha encontrado es que view
es diferente de su UIView
. 'view' se refiere a toda la vista. Por ejemplo, su pantalla de inicio es una vista.
Necesitas separar claramente toda la 'vista' tu ' UIView 'y tu'UILabel'
Puede lograr esto yendo a su storyboard, haciendo clic en el elemento, Inspector de identidad y cambiando el Restoration ID
.
Ahora para acceder a cada elemento en su código usando el ID de restauración
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-12 08:03:14
Es simple en Swift . simplemente ponga este color en su color de vista de fondo y funcionará .
let dimAlphaRedColor = UIColor.redColor().colorWithAlphaComponent(0.7)
yourView.backGroundColor = dimAlphaRedColor
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-11 07:19:43