keeztian keeztian - 3 months ago 10
iOS Question

iOS - Scrollbar of Scrollview shows up but doesn't scroll content

I have included a ScrollView in one of my scenes in my application's storyboard. Now I'm very unfamiliar with many things in iOS and I have just managed to finally make a scrollbar appear in the scene. The problem is, although the scrollbar shows up, it does not scroll on the contents.

I set up the ScrollView in my AppRequestViewController.h and AppRequestViewController.m:

AppRequestViewController.h

@interface AppRequestViewController : UIViewController
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;


AppRequestViewController.m

- (void)viewDidLoad
{
[super viewDidLoad];
self.scrollView.contentSize=CGSizeMake(320,1000);

}


Here is my view hierarchy:

enter image description here

The scene in the storyboard:

enter image description here

The scrollbar in the simulator that shows up but doesn't quite work:

enter image description here

Can someone tell me where I went wrong? Thanks.

Answer

Setting ScrollView for Storyboards in iOS can be a little confusing. The setup of ScrollView in my question is correct, but there are few mistakes that made the program run differently from expected.

First, the fix I found was to uncheck Set Autolayout in the File Inspector. The 2nd one is correcting the View hierarchy. To test my fix, I changed the content of the scene to a simpler one: with View, ScrollView, a nested View, and two buttons. Here's the new View hierarchy:

enter image description here

The ScrollView is on top of the View hierarchy after the main View. Then, I placed another View to hold other View objects. The reason why my program didn't work in the first place is because the ScrollView is in the same level as the other objects that's why it can't scroll the contents.

I also added additional setup in the viewDidLoad method in the AppRequestViewController.m:

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.navigationController setNavigationBarHidden:YES];
    [self.scrollView setScrollEnabled:YES];
    self.scrollView.contentSize=CGSizeMake(320,800);

}

Setting up ScrollView is quite simple, but it can be confusing. I fixed by program based on this YouTube tutorial. Hope this helps.

Comments