1
votes

I have been working on a project in XCode 4.5.2 and have various uiviews set up and looking the way I want them and all working.

On one view, I am needing to now add a uiscrollview, as the content is going to be larger than the page, and will need to scroll vertically.

I have added a uiscrollview, and set the content size, etc, in viewdidload

[scrollView setScrollEnabled:(YES)];
[scrollView setContentSize:CGSizeMake(320,650)];

When I run this and try to scroll vertically, I can see the scroll bar appearing, so the uiscrollview is working and scrolling, but all the content I already added to my uiview stays still and doesnt move.

I have tried adding a small quick test image programmatically to the scrollview

CGRect imageViewFrame = CGRectMake(10,10,10,10);
UIImageView *imageView = [[UIImageView alloc] initWithFrame:imageViewFrame];
imageView.image = [UIImage imageNamed:@"1001_a.jpg"];
[scrollView addSubview:imageView];

And this small image works and scrolls as it should with the scrollview, but all the other previously added content, labels, images, etc, all still stay where they were and dont move at all.

I am assuming this is something to do with subviews, and all my previous existing content is not a subview of the scrollview - but how do I correct this?

I already have my page and uiview laid out in interface builder, and dont really want to delete everything and add all the content programatically as I did with the small test image.

Is there something I need to do in the viewdidload to set the existing content as a subview to the uiscrollview?

[scrollView addSubview:???];

Many thanks for any tips or advice in advance - this is driving me crazy!!

If you need any screenshots or further code, happy to attach.

1

1 Answers

2
votes

The problem is that all your stuff is added to the UIView, not the scrollview. Scrolling the scrollview only scrolls it's children, that's why your test image DOES scroll. If you're building this in a nib, you just need to drag all the stuff that needs to scroll INSIDE the scrollview and it will work. You could also change the controller UIView to be a UIScrollView itself. That's on the 3rd tab in the right bar of utilities, where it says custom class, change it to UIScrollView. You'll lose some versatility by doing it this way but it's the fastest way.