0

To show a help view I've put an UIImageView which behind has some UIButtons. How can I disable user interaction of these buttons? If I touch this image where buttons are behind, they responds to touch events.

CODE FOR IMAGE BACKGROUND:

self.helpBackground = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)];
self.helpBackground.backgroundColor = [UIColor colorWithWhite:0 alpha:0.75];
self.helpBackground.hidden = YES;
[self.view addSubview:self.helpBackground];

I've used self.helpBackground.userInteractionEnabled = NO; but didn't work.

Thanks.

RFG
  • 2,880
  • 3
  • 28
  • 44

4 Answers4

0

Put your buttons in a array and loop through them and disable them:

NSArray *buttonsArray = @[yourButton1, yourButton2];

for (UIButton *button in buttonsArray) {
button.enabled = NO;
}

And when you want them enabled just loop again and set enabled to YES

bangerang
  • 473
  • 1
  • 6
  • 15
0

Keep a Tag to your helpView(imageview) and add the following code

for (UIView *view in self.view.subviews) {
        if (view.tag != yourViewTag) {
            view.userInteractionEnabled = NO;
        }
    } 

And after removing the help screen use the following code

for (UIView *view in self.view.subviews) {
            view.userInteractionEnabled = YES;
    } 
SreeHarsha
  • 496
  • 4
  • 19
0

you can try below solution..When help imageview is appearing

    for (UIView *view in [self.view subviews]) 
    {
        if (view isKindOfClass:[UIButton Class]) 
        {
            view.userInteractionEnabled = NO;
        }
       else
        {
            view.userInteractionEnabled = YES;
        }
    } 

////After dismissing the help screen you can

    for (UIView *view in [self.view subviews]) 
    {
        if (view isKindOfClass:[UIButton Class]) 
        {
            view.userInteractionEnabled = YES;
        }
    } 

////(OR) Simply do as below

self.view.userInteractionEnabled=YES;

Hope it helps you..

Vidhyanand
  • 5,369
  • 4
  • 26
  • 59
0

You can create a method that disables user interaction for all views that are under your helpBackground:

- (void)disableUserInteractionForViewsUnderView:(UIView *)view
{
    CGRect area = view.frame;
    for (UIView *underView in [self.view subviews]) {
        if(CGRectContainsRect(area, underView.frame))
            underView.userInteractionEnabled = NO;
    }
}

and after that you call it where you need it:

[self disableUserInteractionForViewsUnderView:self.helpBackground];

EDIT

I've created a UIViewController category gist on github.com. You can find it here: https://gist.github.com/alexcristea/0244b50e503e8bf4f25d

You can use it like this:

[self enumerateViewsPlacedUnderView:self.helpBackground usingBlock:^(UIView *view) {
    if ([view isKindOfClass:[UIButton class]]) {
        view.userInteractionEnabled = NO;
    }
}];
alexcristea
  • 2,316
  • 16
  • 18