0

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?

Community
  • 1
  • 1
Dafen
  • 1,122
  • 1
  • 9
  • 26

2 Answers2

1

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 .

Dilip Manek
  • 9,095
  • 5
  • 44
  • 56
  • Hi, and what view should be the File's Owner view outlet? Without it doesn't load... Thanks for your help! – Dafen Feb 05 '13 at 12:22
  • you dont have to set it,or if you want than set our main view which contain scrollview set it to main view , i have run this and it work perfectly. – Dilip Manek Feb 05 '13 at 12:26
  • Hey, without the outlet set, the app crashes with this error: Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UIViewController _loadViewFromNibNamed:bundle:] loaded the "NameDetailViewController" nib but the view outlet was not set.' – Dafen Feb 05 '13 at 12:31
  • Can you give me your source code/project folder? I may mess something else up here. Thanks agin for help – Dafen Feb 05 '13 at 12:32
  • you have seted delegate of scrollview to file owner? – Dilip Manek Feb 05 '13 at 12:33
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/23964/discussion-between-dafen-and-dilip) – Dafen Feb 05 '13 at 12:33
0

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..

iphonic
  • 12,615
  • 7
  • 60
  • 107
  • Hi! yeah that works, but how do I load the content of the view in my nib file into the view I add to the scroll view? Here I would just add a empty view, right? – Dafen Feb 05 '13 at 12:12
  • You can even use xib and create UIView component connect with IBOutlet, then design the UIView and instead of [self.contentView addSubView:view]; you do [self.contentView addSubView:self.viewFromOutlet]; all the components will be added with the actual view.. – iphonic Feb 05 '13 at 12:19
  • I think that is what I've done before. The Nib file contains two views, an empty scroll view and a view with all my buttons and stuff. The way it did it, the second view was loaded into the scroll view (witch was connected to the Files Owner view outlet). So the content was shown, but I could not scroll it. I hope I did not misunderstood what you meant. – Dafen Feb 05 '13 at 12:28