skip to Main Content

I have a background image on a ViewController. I also have two buttons inside two UIViews.

I want the views to be white with 0.5 opacity, but the buttons should have 1 opacity.

I have set the opacity on the UIViews like this:

myView.layer.opacity = 0.5

But this also gives the buttons the opacity of 0.5. When I place the buttons above the UIView in storyboard they automatically get embedded in the UIView.

How can I get 1 opacity on the buttons?

P.S. I have tried myButton.layer.opacity = 1

enter image description here.

enter image description here

2

Answers


  1. Opacity can be set in .init method of Color. This should overwrite opacity for that specific button to 1.

    Button(action: {}, 
            label: {
                Image("<your button>")
                    .foregroundColor(Color.init(red: 0, green: 0, blue: 0, opacity: 1))                        
    })
    
    Login or Signup to reply.
  2. The problem with setting the opacity on the view is that all of the subviews will be affected by the same opacity.

    So if you have a view with opacity 0.5 then any subviews will have a maximum effective opacity of 0.5.

    To fix this you can give the view an opacity of 1.0 but then set the background color like…

    view.backgroundColor = Color.white.withAlphaComponent(0.5)
    

    This will still allow the subviews to have an opacity of 1.0.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search