Using Erica Sadun’s scroll view example I created a project.
I now have a scroll view and a page control in my project.
I create a new view for each page and add it to my scroll view as seen below.
Each view has 5 buttons so I can scroll 5 by 5.
However the buttons do not seem to be enabled.
Any ideas why?
// Create the scroll view and set its content size and delegate
sv = [[[UIScrollView alloc] initWithFrame:CGRectMake(0.0f, 200.0f, 300.0f, 60.0f)] autorelease];
sv.contentSize = CGSizeMake(NPAGES * 300.0f, sv.frame.size.height);
sv.pagingEnabled = YES;
sv.delegate = self;
// Load in all the pages
for (int i = 0; i < NPAGES; i++)
{
UIView *menuView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 300.0f, 60.0f)];
for (int j = 0; j < 5 * (i+1); j++)
{
UIImage *menuImage = [UIImage imageNamed:@"menuimage.png"];
UIImage *menuImage2 = [UIImage imageNamed:@"menuimageselected.png"];
UIButton *menuButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
menuButton.frame = CGRectMake(j * 60.0f, 0.0f, 60.0f, 60.0f);
[menuButton addTarget:self action:@selector(menuButtonClicked) forControlEvents:UIControlEventTouchUpInside];
menuButton.tag = j + 5 * i;
menuButton.backgroundColor = [UIColor clearColor];
[menuButton setImage:menuImage forState:UIControlStateNormal];
[menuButton setImage:menuImage2 forState:UIControlStateSelected];
[menuButton setImage:menuImage2 forState:UIControlStateHighlighted];
[menuButton setEnabled:YES];
[menuImage release];
[menuButton release];
[menuView addSubview:menuButton];
}
[sv addSubview:menuView];
[sv bringSubviewToFront:menuView];
[menuView release];
}
[self.view addSubview:sv];
I solved this problem by changing the frame of the view I am adding to my scroll view.
So instead of this line:
I now make the view change its frame every time I change the page:
So the reason was related with the location of the frame of the view I was adding to the scroll view. This problem seems to occur when scroll view is used together with paging. When the page changes make sure the view inside scrollview also moves to the same frame.