How can I have an opaque UIView as a subview of a semi-transparent UIView?

前端 未结 5 1485
时光说笑
时光说笑 2020-12-24 06:47

I have a UIView with an alpha of 0.5 which I add as a subview to my primary view in order to gray-out everything else. I want to add an additional UIView to this gray UIVie

相关标签:
5条回答
  • 2020-12-24 07:12

    Set the UIView background color alpha not it's alpha directly.

    Objective-C

    UIView *view;
    ...
    view.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:.6];
    

    It's not the same as:

    view.backgroundColor = [UIColor blackColor];    
    view.alpha = .6;
    

    Swift

    view.backgroundColor = UIColor.black.withAlphaComponent(0.6)
    
    0 讨论(0)
  • 2020-12-24 07:13

    No, not really. What you want is to take your overlay view, and make it just have a clear background color. As a subview of that new overlay place your view that will grey things out. And as a sibling view to that put your view you want to be opaque.

    [OpaqueView] [DimmingView]
         |             |
          [OverlayView]
    
    0 讨论(0)
  • 2020-12-24 07:19

    No, any view will inherit the opacity of its parent.

    0 讨论(0)
  • 2020-12-24 07:25

    Don't put it inside the semi-transparent view. Make it a sibling to semi-transparent view and put it over it using z-ordering.

    0 讨论(0)
  • 2020-12-24 07:27

    This will only work if you have any image on the background.

    Instead of reducing the alpha of UIView, add an UIImageView on that view and then reduce the alpha of the UIImageView.

    now add your subviews on the UIView.

    your subviews will not take the alpha property anymore.. :)

    0 讨论(0)
提交回复
热议问题