Objective-c – UIButton inside a UIView not working after UIView is animated

objective cuibuttonuiviewanimation

I have seen many questions in this forum which gives answer to this topic "UIButton inside a UIView, when animated doesn't work", but after having tried out the answers like

a) UIViewAnimationOptionAllowUserInteraction to the options
b) subView.setUserInteractionEnabled = YES
c) [button addTarget:self action:@selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];

none of them is working for me 🙁

Here is the scenario. App is in landscape mode and a UIView called menuView is placed at x=480,y=0. Its height=200 and width=150. So, when I tap the button on the top right corner, the following code is executed

- (IBAction)showMenu {
    [menuView setUserInteractionEnabled:YES];
    [optionsButton setUserInteractionEnabled:YES];
    [UIView animateWithDuration:0.5 delay:0.0 options:UIViewAnimationCurveEaseOut |  UIViewAnimationOptionAllowUserInteraction
                     animations:^{
                         self.view.frame = CGRectOffset(self.view.frame, 0, 200);
                     }
                     completion:NULL];
}

Output?: View is appearing, but optionsButton inside the menuView is not clickable. I hooked up to an event, did an NSLog(@"Options button clicked"), but nothing happens 🙁

Please tell me what I am doing wrong?

Update: When I place the subview that is "menuView" inside the self.view, then upon running and click the optionsButton, I get the NSLog message. Only if I specify the origin.x of menuView to be 480 and above, it doesn't work.

Best Answer

If you can see the UIButton, its userInteractionEnabled (YES by default!) is set, and its superview's userInteractionEnabled in the whole hierarchy is YES - you should be able to interact with it.

An animation will never change your userInteractionEnabled property! So what you are doing there is simply unnecessary. If you try to enable interaction during animation - that's a different story and is just an option passed to the animation message.

So if that's not your problem (and is probably not), then I guess one of the superview's frame is cropping the UIButton!

Now you see, if a UIButton (or any UIView) is outside a UIView's frame, it can still be visible, unless clipsToBounds is set on the superview.

And the outcome of that situation is: You can see me, but you can't touch me.

Related Topic