I'm struggling to understand a few questions that you might find simple about UIScrollView
.
Why do we increase the
contentSize
and not the bounds of theUIScrollView
? Actually, I don't understand what the bounds andcontentSize
do.When I set
pagingEnabled
to YES, how does the scrollView know where to stop scrolling?I want to add a gap between pages in my
UIScrollView
. (pagingEnabled = YES
) I searched on the Internet and I found the following code, that rob mayoff wrote:
- (void)viewDidLoad { [super viewDidLoad]; NSArray *colors = [NSArray arrayWithObjects:[UIColor redColor], [UIColor greenColor], [UIColor blueColor], nil]; #define kGutterWidth 20 UIScrollView *scrollView = self.scrollView; CGRect scrollViewFrame = scrollView.frame; scrollViewFrame.size.width += kGutterWidth; scrollView.frame = scrollViewFrame; CGSize scrollViewSize = scrollView.bounds.size; for (int i = 0; i < colors.count; i++) { CGRect frame = CGRectMake(scrollViewSize.width * i, 0, scrollViewSize.width - kGutterWidth, scrollViewSize.height); UIView *subview = [[UIView alloc] initWithFrame:frame]; subview.backgroundColor = [colors objectAtIndex:i]; [scrollView addSubview:subview]; [subview release]; } scrollView.contentSize = CGSizeMake( colors.count * scrollViewSize.width, scrollViewSize.height); }
The source of the code: How to create a paging scrollView with space between views
This code works properly, but it doesn't matter as long as I don't understand it.
I know that eventually the pages will be loaded with their original size, but I don't understand how does it happen, because each page is subtracted with kGutterWidth.
Why did rob increase the scrollView's frame with kGutterWidth?
OK, this questions might be sounded as stupid, but I really don't understand it. In the code, Rob created a new scrollView object that was called
scrollView
. It's value is equal toself.scrollView
value. Well, I don't understand how changes that are done toscrollView
, impactself.scrollView
, because they are different objects. Their memory addresses are different.
I'm struggling to understand these questions for a long time. Today I've decided to write them here.
Thank you.