user1118374
user1118374

Reputation: 174

UIButton as a subview not responding

I have a UIButton inside of a UIView inside of a UIImageView that is not responding to touches. I can't figure out why. I've broken the code down to its simplest form while still doing what my original program does.

#import "TestVideoViewController.h"

@interface TestVideoViewController ()

@property (strong, nonatomic)UIImageView *panel;
@property (strong, nonatomic)UIView *panelSC;
@property (strong, nonatomic)UIButton *panelButtonSC;
@property (strong, nonatomic)UIButton *videoButton;

@end

@implementation TestVideoViewController

-(void) viewDidLoad {

    _panelButtonSC  = [UIButton buttonWithType:UIButtonTypeCustom];
    [_panelButtonSC addTarget:self action:@selector(buttonPressedSC:) forControlEvents:UIControlEventTouchUpInside];
    [_panelButtonSC setTitle:@"Open" forState:UIControlStateNormal];
    _panelButtonSC.frame = CGRectMake(511, 701, 66, 67);

    _panel = [[UIImageView alloc] initWithFrame:CGRectMake(100, 768, 824, 600)];
    _panel.backgroundColor = [UIColor whiteColor];

    _panelSC = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 784, 560)];
    _panelSC.backgroundColor = [UIColor whiteColor];

    _videoButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_videoButton addTarget:self action:@selector(playVideo:) forControlEvents:UIControlEventTouchUpInside];
    [_videoButton setTitle:@"Play" forState:UIControlStateNormal];
    [_videoButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    _videoButton.frame = CGRectMake(400, 400, 200, 40);

    [_panelSC addSubview:_videoButton];

    [_panel addSubview:_panelSC];

    [self.view addSubview:_panel];
    [self.view addSubview:_panelButtonSC];

}

- (IBAction)buttonPressedSC:(UIButton*)sender {

    [self animatePanelUp];
}

- (IBAction)playVideo:(UIButton*)sender {

    NSLog(@"play video");
}

- (void) animatePanelUp {
    [UIView animateWithDuration: 0.5
                          delay: 0.0
                        options: UIViewAnimationOptionCurveEaseIn
                     animations:^{
                         _panel.frame = CGRectMake(47, 166, 929, 602);
                     }
                     completion:nil];

}

@end

I've looked at every similar answer here. To the best of my knowledge all of my framing is correct. There has got to be something simple and stupid that I'm missing. Why doesn't the "playVideo" method ever fire? The only time it works is if I add the button directly to self.view.

Upvotes: 2

Views: 3484

Answers (3)

Saqib Saud
Saqib Saud

Reputation: 2795

Make sure user interaction is enabled of the parent view. UIImageView by default has userInteractionEnabled false.

_panel.userInteractionEnabled = true

Upvotes: 0

Girish Kolari
Girish Kolari

Reputation: 2515

UIImageView keeps userInteractionEnabled as false by default - add following line of code it will work.

_panel.userInteractionEnabled = true;

Upvotes: 8

Rashad
Rashad

Reputation: 11197

As you are adding a few subview upon one another, check that your button is clickable and no other subview is eating up the touch event, and overall make sure that your views are user action enabled.

Hint:

There might have some problem with the imageview used, as it will have user interaction disabled by default.

Use UIView instead or do this:

_panelSC.userInteractionEnabled = true;

Upvotes: 2

Related Questions