The following is an example of how to create a bookshelf. Let's talk about the differences between contentsize, contentoffset, and contentInset.
[Plain]
NSString * path = nil;
UIImage * back = nil;
NSString * str = nil;
UIImage * aImage = nil;
Path = [[NSBundle mainBundle] pathForResource: @ "shujia" ofType: @ "jpg"];
Back = [UIImage imageWithContentsOfFile: path]; // load the image
_ ScrollView = [[UIScrollView alloc] initWithFrame: CGRectMake (96, 84, back. size. width, back. size. height)];
_ ScrollView. showsVerticalScrollIndicator = NO; // hide the scroll bar in the vertical direction.
// Use shujia.jpg as the background of _ scrollerView
UIColor * color = [[UIColor alloc] initWithPatternImage: back];
[_ ScrollView setBackgroundColor: color];
_ ButtonList = [NSMutableArray arrayWithCapacity: 9];
For (int I = 0; I <9; I ++)
{
Str = [NSString stringWithFormat: @ "menubook % d", I + 1];
Path = [[NSBundle mainBundle] pathForResource: str ofType: @ "png"];
AImage = [UIImage imageWithContentsOfFile: path];
Int x = (I % 3) * (aImage. size. width + 15 );
Int y = (I/3) * (aImage. size. height + 30 );
_ WhichPeople = [UIButton buttonWithType: UIButtonTypeCustom];
_ WhichPeople. frame = CGRectMake (x + 10, y + 30, aImage. size. width, aImage. size. height );
_ WhichPeople. tag = I;
[_ WhichPeople setImage: aImage forState: UIControlStateNormal]; // This image is returned when you click a cloth.
Path = nil;
AImage = nil;
Str = nil;
Str = [NSString stringWithFormat: @ "menubook % d_1", I + 1];
Path = [[NSBundle mainBundle] pathForResource: str ofType: @ "png"];
AImage = [UIImage imageWithContentsOfFile: path];
[_ WhichPeople setImage: aImage forState: UIControlStateHighlighted]; // response when the image is clicked
[_ WhichPeople addTarget: self action: @ selector (ButtonClick :) forControlEvents: UIControlEventTouchUpInside];
[_ ScrollView addSubview: _ whichPeople];
[_ ButtonList addObject: _ whichPeople];
Path = nil;
AImage = nil;
Str = nil;
}
// Control the scrolling Area
_ ScrollView. contentSize = CGSizeMake (480,580 );
// Offset of the vertex in the current display area relative to the frame Vertex
_ ScrollView. contentOffset = CGPointMake (0, 0 );
// Position of the vertex of the contentview of scrollview relative to the scrollview
_ ScrollView. contentInset = UIEdgeInsetsMake (0, 0, 0, 0 );
[Self. view addSubview: _ scrollView];
ContentSize is the area where scrollview can scroll. For example, frame = (0, 0,320,480) contentSize = (320,960) indicates that your scrollview can scroll up or down, and the rolling area is twice the frame size.
ContentOffset is the offset of the vertex in the current display area of the scrollview to the frame vertex. For example, if you pull to the bottom of the last example, contentoffset is (0,480), that is, y offset is 480.
ContentInset is the position of the contentview vertex of the scrollview relative to the scrollview. For example, if your contentInset = (0,100), your contentview is displayed starting from the scrollview (0,100 ).
From the ComeOnTom Column