Setting alpha on UIView sets the alpha on its subviews which should not happen

前端 未结 10 2355
借酒劲吻你
借酒劲吻你 2020-12-13 01:29

According to the documentation for UIVIew @property(nonatomic) CGFloat alpha

The value of this property is a floating-point number in the

相关标签:
10条回答
  • 2020-12-13 02:07

    Simplest solution as discussed is to change the alpha as follows : Updated version for Xcode 8 Swift 3 is :

    yourParentView.backgroundColor = UIColor.black.withAlphaComponent(0.4)
    

    Objective C:

    yourParentView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    

    Refer Apple Developer Docs here : https://developer.apple.com/reference/uikit/uiview/1622417-alpha

    0 讨论(0)
  • 2020-12-13 02:08

    Here is a bit complex solution:

    UIView *container;
    UIView *myView;
    UIView *anotherView;
    
    myView.alpha = 0.5;
    [container addSubview:myView];
    
    anotherView.alpha = 1;
    [container addSubview:anotherView];
    

    Use a container view as superview, anotherView and myView are both subview in container, anotherView is not a subview in myView.

    0 讨论(0)
  • 2020-12-13 02:11

    In swift

    view.backgroundColor = UIColor.whiteColor().colorWithAlphaComponent(0.5)
    

    UPDATED FOR SWIFT 3

    view.backgroundColor = UIColor.white.withAlphaComponent(0.5)
    
    0 讨论(0)
  • 2020-12-13 02:13

    Don't set the alpha directly on the parent view. Instead of it use the below line of code which will apply transparency to parentview without affecting its child views.

    [parentView setBackgroundColor:[[UIColor clearColor] colorWithAlphaComponent:0.5]];

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