UIButton inside UIView, with showsTouchWhenHighlighted glow is clipped by superview

1.1k Views Asked by At

it's possible the approach i'm using with this might be a little wrong, and if so perhaps someone can advise... but in any case, a pattern i've got into for creating responder widgets involves adding an invisible UIButtonTypeCustom that fills the bounds of a UIView in order to add backwards-compatible button behaviour to any view. for my needs, this works perfectly well. in order to slightly improve the user experience of using this hybrid button, i set showsTouchWhenHighlighted to YES for the button. this works fine, but the "glow" of the button is clipped by the button's parent UIView. i've tried;

button.layer.masksToBounds = NO;
button.clipsToBounds = NO;
parentView.layer.masksToBounds = NO;
parentView.clipsToBounds = NO;

but none of these allow the glow to escape the bounds of the parentView. it's quite a minor cosmetic issue, but i think it would look nicer to get this working

hope someone can help! thanks

1

There are 1 best solutions below

0
On

I use code like this, and the glow is not clipped by the parent - verified and tested.

UIButton myButton = [[UIButton alloc] init....];
[parentView addSubview:myButton];
myButton.showsTouchWhenHighlighted = YES;
parentView.clipsToBounds = NO;

No need to set .clipsToBounds = NO on the button; the default is already NO.

Have you checked that parentView is non-nil when you're setting the properties? Check also that there isn't another UIView further up in the hierarchy that might be clipping the glow effect.