How do you darken a view as if it were disabled/highlighted, preferably without using any additional views?
By view I mean a UIView
, with all its children. I want to achieve the same effect of a disabled/highlighted UIButton
.
Do not assume that the view is fully opaque.
Example:
You have a UIButton *button1 and UIView *view1
You can disable a button and a view in this way:
Enabling a button and a view can be done this way:
Hope this helps..
While this approach fails to meet your preference of not using additional views, simply adding a black view with alpha of 0.6 or so seems to achieve the effect, with the added benefit that you can use this new view to intercept UIEvents so that all the subviews are effectively disabled en masse.
You can even get graphically fancy and instead of just using a black background for the overlaid view, you can fill its background with a radial gradient to achieve the sort of spotlighted effect that happens in iOS when a popup view disables the view behind it...
What I'm currently playing with:
_highlightLayer
). This is similar to the "black view with alpha" approach._highlightLayer
with an non-opaque image of the original view._highlightLayer
to the view's layer.Only the non-transparent pixels of the view will be darkened.
The code:
And then:
Of course, this should only be used for small views.