简体   繁体   中英

Click on Disabled button on top of other button triggers the other button's click event

I have a disabled button on top of enabled button:

禁用按钮

The green button is disabled. When I click it, it triggers a click on the blue button behind.

I tried:

self.testButton.enabled = YES;
self.testButton.userInteractionEnabled = NO;

But I still got the same behavior

Is there any way to prevent the blue button click event in this scenario?

Edit:

Fixed by not really disabling the button but changing it's target to nil:

        [myButton addTarget:nil action:nil forControlEvents:UIControlEventTouchUpInside];
        [myButton setImage:[UIImage imageNamed:@"disabledImage.png"] forState:UIControlStateNormal];

Yes, blue button will always get an action as green button is not enable or intractable.

So, here you need one overlay between these two buttons to stop tap event of green button.

How ever below approach is not efficient way to tackle down this scenario, but you can use it unless you will find better one.

Add green button in a UIView (Which work as an overlay between these two views) with same size as green button, add green button as a subview of this view and add this view at place of green button.

So that whenever you disable green button and tap on it. Tap will be on UIView which is a superview of green button.

Use view hierarchy :

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM