Weird thing happening when adding UIViews to a ScrollView
I have a problem when adding UIViews to my scrollview:
NSArray *views = [NSArray arrayWithObjects:view1, view2, view3,nil];
for (int i = 0; i < views.count; i++) {
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
[self.scrollView addSubview:[views objectAtIndex:i]];
}
self.pageControl.numberOfPages = views.count;
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * views.count, self.scrollView.frame.size.height);
That's the code I'm using. The problem is that the ScrollView has the three pages, and all, but just the first page actually contains a view, which is the last one in the array. Why is this happening? I also tried:
for (UIView *view in views) {
[self.scroll开发者_如何学GoView addSubview:view];
}
With the same result. What I'm doing wrong?
You need to set the position of the views you add:
[views objectAtIndex:i].frame = frame;
You are currently not changing the view's position anywhere, so all of them are probably at (0|0).
You're initializing frame in your loop but omit to do anything with it. You need to replace each view's frame to use it.
精彩评论