I am attempting to make a basic scroll view with a connected page control, and I have run into some problems with my code. My viewDidLoad method looks like this:
- (void)viewDidLoad{
[super viewDidLoad];
NSLog (@"View did load");
// Do any additional setup after loading the view, typically from a nib.
scrollView.delegate = self;
scrollView.pagingEnabled=YES;
NSArray *frames = [NSArray arrayWithObjects:[UIColor redColor], [UIColor greenColor], [UIColor blueColor], nil];
for (int i = 0; i < frames.count; i++) {
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
UIView *subview = [[UIView alloc] initWithFrame:frame];
subview.backgroundColor = [frames objectAtIndex:i];
[self.scrollView addSubview:subview];
NSLog (@"Looped, i = %i color = %@", i, [frames objectAtIndex:i]);
}
self.scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * frames.count, scrollView.frame.size.height);
NSLog(@"The width is: %f", self.scrollView.frame.size.width);
}
The problem seems to be that the coloured frames I attempt to add to the view never appear (not even the first one). If I didn’t have ARC enabled, I would have released the CGRect frame at the end of each loop, but it really should do that itself with ARC, right?
When I run the app, both in the simulator and on my actual phone, I get a black screen with the UIPageControl dots at the bottom. When I swipe my finger across the ScrollView nothing happens, but if I pull it very far to one side, the pageControl moves to the next “dot”.
The last NSLog sentence always outputs 0.0000, indicating that none of the views have been added to the scroll view.
All help is very appreciated 🙂
The problem is doing it in viewDidLoad. Move your code to viewDidAppear: and it should work fine. Frames are not yet set in viewDidLoad.