简体   繁体   中英

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?

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 .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM