11

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.

thandasoru
  • 1,558
  • 2
  • 15
  • 41
  • 1
    Perhaps it's nil because you haven't hooked up an outlet. – CodaFi Mar 03 '13 at 09:17
  • 1
    which hasn't been hooked up to an outlet? If you are talking about the button or view, I have hooked bot up to an outlet in my .h file @ property (weak, nonatomic) IBOutlet UIView *menuView; @ property (weak, nonatomic) IBOutlet UIButton *optionsButton; – thandasoru Mar 03 '13 at 09:19
  • 1
    Set a breakpoint and be sure, then. – CodaFi Mar 03 '13 at 09:23
  • 1
    can you try bringing the button to front after `self.view.frame = CGRectOffset(self.view.frame, 0, 200);` and let me know the result – DD_ Mar 03 '13 at 09:53
  • 2
    I did this and it doesn't work [self.view bringSubviewToFront:optionsButton]; – thandasoru Mar 03 '13 at 10:26

4 Answers4

29

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.

daniel.gindi
  • 3,457
  • 1
  • 30
  • 36
  • 1
    Ok, I have made the width of the UIButton inside the UIView (menuView) to be 185. The width of menuView is 200. How do I find out if superview's frame (whose width is 480) is cropping the UIButton? Can you please help me here? – thandasoru Mar 03 '13 at 15:09
  • 1
    If I place the menuView inside my self.view and run the app, it works. Only if I place menuView outside and try to offset the self.view.frame by 200, then it does not work. I think it maybe due to the fact that the menuView is not inside self.view. so if I increase the width of self.view to accommodate the menuView, then it should work, right? – thandasoru Mar 03 '13 at 16:52
  • 1
    If you are placing menuView "outside" the self.view - then where is it exactly? Can you describe the hierarchy of views precisely? – daniel.gindi Mar 03 '13 at 16:57
  • 1
    self.view is landscape. It's width is 480, height is 300. menuView's origin.x is 480 and width is 200. There is a button on self.view at x=427. so I touch the button, self.view moves left by the width of menuView. Have I described correctly? – thandasoru Mar 03 '13 at 17:01
  • This is absolutely weird. I added this code to increase the size of the self.view and the optionsButton started working. > [self.view setFrame:CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, self.view.frame.size.width, 580)]; > [self.view setFrame:CGRectOffset(self.view.frame, 0, 100)]; – thandasoru Mar 03 '13 at 17:40
  • 2
    Yeah, it makes sense. I cannot tell if menuView is subview of self.view or not, and what are the exact measures, but NSLogging the views themeselves, along with their subviews array and superview property, can give the full picture. Anyway - you see that the only (simple) possible explanation, is what works. Your UIButton was out of bounds. I can't tell out of which view's bounds, but still :-) – daniel.gindi Mar 03 '13 at 21:11
8

In my case missing a constraint on the height of the containing view caused the view height to be 0. Buttons were visible, below the view, but untouchable.

Dan Loughney
  • 4,647
  • 3
  • 25
  • 40
1

For me it was all about an overlooked mistake in the animation code itself. Where my new height was being called I was actually calling my new width, and where my new width was being called I was actually calling my new height, therefor causing the UIView to be super long yet super narrow, thus pushing all content within the center of view, outside of the view reach.

  • Change your UIViews background color to Red so that you can actually see where it animates to.

  • If your UIView is within another view, turn on clipSubviews so that you only see what is actually enabled within the subView.

  • Run your project.

If your UIView is not its normal height and width, more than likely you have overlooked some code in your animation call.

Make sure that your x, y, width, height are being shown in the correct orders within the animation code.

Gustavo Morales
  • 2,614
  • 9
  • 29
  • 37
Felecia Genet
  • 367
  • 4
  • 3
0

In my case, it seems like my animated transitioning is not yet completed. I forgot to to put completeTransition:Bool at the end of my animateTransition:context method.

John Paul Manoza
  • 1,735
  • 25
  • 22