I’ve scrollview with many image button, for the button 1-8 there’s no problem on button behaviour. it can be log which is clickable.
the problem is button on 2nd page look like not listening on selector (button 9-10). it possible problem from pagecontrol? or i missed something here?
// button paging
myImages = [NSArray arrayWithObjects:
[UIImage imageNamed:@"img_1.png"],
[UIImage imageNamed:@"img_2png"],
[UIImage imageNamed:@"img_3.png"],
[UIImage imageNamed:@"img_4.png"],
[UIImage imageNamed:@"img_5.png"],
[UIImage imageNamed:@"img_6.png"],
[UIImage imageNamed:@"img_7.png"],
[UIImage imageNamed:@"img_8.png"],
[UIImage imageNamed:@"img_9.png"],
[UIImage imageNamed:@"img_10.png"],
nil
];
// prepare counter
int posX, posY = 5, counter = 0, pages = 0;
BOOL reset = NO;
// uiviewscroller
CGRect scrollFrame;
scrollFrame.origin.x = self.myScrollView.frame.size.width * pages;
scrollFrame.origin.y = 0;
scrollFrame.size = self.myScrollView.frame.size;
UIView *subView = [[UIView alloc] initWithFrame:scrollFrame];
subView.backgroundColor = [UIColor colorWithRed:232.0f/255.0f green:233.0f/255.0f blue:235.0f/255.0f alpha: 1];
// loop over images
for (int i = 0; i < myImages.count; i++)
{
counter++;
// reposition X
if (counter == 1 || counter == 5)
{
posX = 4;
// more than 1 page
if (pages > 0)
posX = 320 * pages;
}
else
{
posX += 80;
}
// reposition Y
posY = (counter <= 4) ? 5 : 85;
if (reset == YES)
{
// reflag
reset = NO;
// redefined size
scrollFrame.origin.x = self.myScrollView.frame.size.width * pages;
}
// create image view for button
UIImage *myIcon = [myImages objectAtIndex:i];
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
// manipulate button behavior
myButton.frame = CGRectMake(posX, posY, 80, 80);
[myButton setTitle:[NSString stringWithFormat:@"%d", i] forState:UIControlStateNormal];
[myButton setImage:myIcon forState:UIControlStateNormal];
[myButton addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
// add image to subview
[subView addSubview:myButton];
if (counter == 8)
{
// reset position
posX = 4;
posY = 5;
// populate counter
counter = 0;
pages++;
// reset section
reset = YES;
// add subview to view
[self.myScrollView addSubview:subView];
}
// add balance subview to scroller
if (counter > 0 && i == myImages.count-1)
{
// add subview to view
[self.myScrollView addSubview:subView];
pages++;
}
}
scrollFrame.origin.x = self.myScrollView.frame.size.width * pages;
self.myScrollView.contentSize = CGSizeMake(self.myScrollView.frame.size.width * pages, self.myScrollView.frame.size.height);
self.myPaging.numberOfPages = pages;
You are adding buttons on a view called subView. Last two buttons are getting added outside the bounds of this view as view’s frame is equal to scrollview’s frame (
UIView *subView = [[UIView alloc] initWithFrame:scrollFrame];).Change its frame after adding buttons-
Add the line below at the end where you are changing content size of scroll view.