Carlos López
Carlos López

Reputation: 759

Can't click on UIButton after add a subview

I'm using a method to add a shadow to my buttons. The problem is after that, I can't click in them. I add the userinteractionenabled property to YES, but still can't tap.

This is the code.

How can I resolve that? Am I missing something?

- (UIView*)putView:(UIView*)view insideShadowWithColor:(UIColor*)color andRadius:(CGFloat)shadowRadius andOffset:(CGSize)shadowOffset andOpacity:(CGFloat)shadowOpacity
{
    CGRect shadowFrame; // Modify this if needed
    shadowFrame.size.width = 0.f;
    shadowFrame.size.height = 0.f;
    shadowFrame.origin.x = 0.f;
    shadowFrame.origin.y = 0.f;
    UIView * shadow = [[UIView alloc] initWithFrame:shadowFrame];
    shadow.userInteractionEnabled = YES; // Modify this if needed
    shadow.layer.shadowColor = color.CGColor;
    shadow.layer.shadowOffset = shadowOffset;
    shadow.layer.shadowRadius = shadowRadius;
    shadow.layer.masksToBounds = NO;
    shadow.clipsToBounds = NO;
    shadow.layer.shadowOpacity = shadowOpacity;
    [view.superview insertSubview:shadow belowSubview:view];
    [shadow addSubview:view];
    return shadow;
}

Upvotes: 0

Views: 1780

Answers (3)

Carlos López
Carlos López

Reputation: 759

I find the solution

After the comments of the people, I did some changes. This is the final code

+ (void)putView:(UIView*)view insideShadowWithColor:(UIColor*)color andBlur:(CGFloat)shadowRadius andOffset:(CGSize)shadowOffset andOpacity:(CGFloat)shadowOpacity
{
    CGRect shadowFrame = view.frame;
    UIView * shadow = [[UIView alloc] initWithFrame:shadowFrame];
    shadow.backgroundColor = [UIColor redColor];
    shadow.userInteractionEnabled = YES; // Modify this if needed
    shadow.layer.shadowColor = color.CGColor;
    shadow.layer.shadowOffset = shadowOffset;
    shadow.layer.shadowRadius = shadowRadius;
    shadow.layer.cornerRadius = view.layer.cornerRadius;
    shadow.layer.masksToBounds = NO;
    shadow.clipsToBounds = NO;
    shadow.layer.shadowOpacity = shadowOpacity;
    [view.superview insertSubview:shadow belowSubview:view];
}

With this, you can have a view with round corners and shadows at the same time. And, of course, the touch events enabled!

Upvotes: 1

Rashad
Rashad

Reputation: 11197

CGRect shadowFrame; // Modify this if needed
shadowFrame.size.width = 0.f;
shadowFrame.size.height = 0.f;
shadowFrame.origin.x = 0.f;
shadowFrame.origin.y = 0.f;
UIView * shadow = [[UIView alloc] initWithFrame:shadowFrame];

In the above code you have a view with frame 0,0,0,0. So how can you tap it?

Upvotes: 0

cph2117
cph2117

Reputation: 2681

That makes sense because you've put a UIView on top of the button, which means that you can't access that button. You've set UserInteractionEnabled on the UIView, but that's useless because you haven't asked the UIView to respond to touch events. In this instance, I would suggest adding a UIGestureRecognizer to the UIView instead of trying to put a UIView on top of a button.

Upvotes: 0

Related Questions