Dafen
Dafen

Reputation: 1150

UIScrollView won't scroll when I add my view

I want to built a large view in IB and then add it to my scroll view. To do so, I have followed these instructions.

My view is correctly drawn in the scroll view, but it cannot scroll. I logged the content size and it's all correct: Content view size: 320.000000 x 714.000000

When I replace [self.view addSubview:self.contentView]; with [self.view addSubview:[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 1)]]; the scroll view is, as expected, empty, but at least the scroll bars are shown and I can scroll. Is there's something wrong with my view in the nib file?

Here is my viewDidLoad:

[super viewDidLoad];
[self.navigationController setToolbarHidden:YES animated:YES];

NSLog(
    @"Content view size: %f x %f",
    _contentView.frame.size.width,
    _contentView.frame.size.height);
[self.view addSubview:self.contentView];
((UIScrollView *)self.view).contentSize = self.contentView.frame.size;

The only difference in my code is, that I have the scroll view inside a navigation controller. Does that make a difference?

Upvotes: 0

Views: 2353

Answers (2)

Dilip Manek
Dilip Manek

Reputation: 9143

Problem is in 4 th point which said "Set the File's Owner's view outlet to the scroll view." instead of it set outlet of scrollview in your header file. And change code of viewDidLoad like this:

- (void)viewDidLoad
{
    [super viewDidLoad];
    [scrollview addSubview:self.contentView];
    scrollview.contentSize = self.contentView.frame.size;
}

Then its work perfectly .

Upvotes: 1

iphonic
iphonic

Reputation: 12717

It should be like below

//Considering self.contentView as UIScrollView added in xib
UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0,0,320,700)];
[self.contentView addSubView:view];
[self.contentView setContentSize:CGSizeMake(320,700)];

The above code should work for sure..

Upvotes: 0

Related Questions