UIScrollview - Off-screen buttons do not fire

Viewed 1044

I have a UIScrollview that is very long and exceeds the width of the screen. See the below screenshot for reference.

example drawing

I am programatically adding buttons to this scrollview in a dynamically-varying for-loop. There is a "containerView" that basically is the same width of the scrollview, and I'm actually adding the buttons directly onto that. The code is pretty straight-forward there, something like the following:

for (int i=0; i<numberOfButtons; i++) {
    UIButton *currentButton = [[UIButton alloc] init];
    [currentButton setTranslatesAutoresizingMaskIntoConstraints:NO];
    [currentButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
    [currentButton setTag:i];
    [currentButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [self.containerView addSubview:currentButton];
    // here's where I programatically set constraints for positioning
}

The buttons show perfectly fine in the correct spots, and the ones initially on the screen (i.e. the first 2 in the screenshot example above) fire the "buttonPressed:" method perfectly fine. However, the ones that are initially off-screen still show fine, but the event is not fired when tapped.

I see other similar posts to this out there, but nothing seems to work. Any ideas on where to go on this one? I'm a bit stumped. Thanks in advance!

2 Answers
Related