How to set dynamically content size of a UIScrollView
I'm working on an app that uses a UIScrollView
to display an image and some text. I have the whole thing to work properly when i set a fixed size for the UIscrollView
ex:
[scrollview setContentSize:(CGSizeMake(320, 480))];
My problem is that the text is fed via some web services an the size varies so i guess i will have to set the content size dynamically, can somebody please tell me how to do that. For info the size of the image 开发者_如何学运维is fixed. Thank you in advance.
You need scrollview to set content size, so first of all set deragand drop uiscrollview in view and set IBOutlat and deleget and than after viewdidload in set content size. like that..
if (PhotoPathArray.count > 0)
{
int y=5,x=-87;
for(int i=0;i<[PhotoPathArray count];i++)
{
if(i%3==0 && i!=0)
{
y=y+100;
x=13;
scroll.contentSize=CGSizeMake(320, y+130);
}
else
{
x=x+100;
}
UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
btn.tag=i;
[btn addTarget:self action:@selector(imegbtnclick:) forControlEvents:UIControlEventTouchUpInside];
btn.frame=CGRectMake(x,y, 95,95);
[scroll addSubview:btn];
}
}
;
// Calculate scroll view size
float sizeOfContent = 0;
int i;
for (i = 0; i < [myScrollView.subviews count]; i++) {
UIView *view =[myScrollView.subviews objectAtIndex:i];
sizeOfContent += view.frame.size.height;
}
// Set content size for scroll view
myScrollView.contentSize = CGSizeMake(myScrollView.frame.size.width, sizeOfContent);
精彩评论