How to set size of UIScrollView frame programmatically? How to set size of UIScrollView frame programmatically? xcode xcode

How to set size of UIScrollView frame programmatically?


CGRect scrollFrame = CGRrectMake(x, y, w, h);ScrollView.frame = scrollFrame;

Or if you need only to change the width and height;

CGRect scrollFrame;scrollFrame.origin = ScrollView.frame.origin;scrollFrame.size = CGSizeMake(w, h);ScrollView.frame = scrollFrame;

Edit keeping the center unchanged:

CGRect scrollFrame;CGFloat newX = scrollView.frame.origin.x + (scrollView.frame.size.width - w) / 2;CGFloat newY = scrollView.frame.origin.y + (scrollView.frame.size.height - y) / 2;scrollFrame.origin = CGPointMake(newX, newY);scrollFrame.size = CGSizeMake(w, h);scrollView.frame = scrollFrame;


you need to do this, for example.

ScrollView.frame = CGRectMake(0, 0, BoardWidth, BoardHeight);

Once done you need also to set contentSize property. It's the area that 'extends' behind the frame of your UIScrollView.

If you want to scroll only horizontally:

ScrollView.contentSize = CGSizeMake(w * BoardWidth, BoardHeight);

If you want to scroll only vertically:

ScrollView.contentSize = CGSizeMake(BoardWidth, h * BoardHeight);

If you want to scroll both horizontally and vertically:

ScrollView.contentSize = CGSizeMake(w * BoardWidth, h * BoardHeight);

where w and h are generic values to increment your width and/or your height.

If you want have an horizontal (for example) discrete scroll (e.g. you have some pages with the same dimension of your screen).

ScrollView.pagingEnabled = YES;ScrollView.contentSize = CGSizeMake(p * BoardWidth, BoardHeight);

where p is the number of pages you want to display.

Hope it helps.


You can do it as below,

scrollView.frame = CGRectMake(xOrigin,yOrigin,width,height);

If you are looking for content size of scrollview then it as below,

scrollView.contentSize = CGSizeMake(width,height);