Reputation: 1104
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:
The scene in the storyboard:
The scrollbar in the simulator that shows up but doesn't quite work:
Can someone tell me where I went wrong? Thanks.
Upvotes: 3
Views: 2753
Reputation: 1104
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:
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 my program based on this YouTube tutorial. Hope this helps.
Upvotes: 4
Reputation: 403
Your content size is correct for scroll view but check the size of view controller in storyboard. Set the simulated size of view controller from freedom to fixed. I think it will help you to solve your problem.
Upvotes: 1
Reputation:
Use viewDidAppear
instead of viewDidLoad
. It will definitely work.
(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self.scrollView setContentSize:CGSizeMake(...)];
}
Upvotes: 1
Reputation: 3090
It seems you scroll view has exceed the screen. do you set the scroll view's autoResizesSubviews
property as true and autoresizingMask
as UIViewAutoresizingFlexibleWidth| UIViewAutoresizingFlexibleWidth
. If you use auto layout
, go to set proper constraints.
Upvotes: 0
Reputation: 1710
I am guessing, setContentSize: inside viewWillAppear: or viewDidAppear: will fix your problem.
- (void)viewDidLoad
{
[super viewDidLoad];
// relocate this line
// self.scrollView.contentSize=CGSizeMake(320,1000);
}
...
- (void)viewWillAppear:(BOOL) animated
{
[super viewWillAppear:animated];
self.scrollView.contentSize=CGSizeMake(320,1000);
}
Upvotes: 2
Reputation: 2978
Scrollable contents should be subview of the scrollview. you will need to check hierarchy of the views first. After that, you may use below code to compare the view size and the content size in your ViewController.
- (void)viewWillLayoutSubviews
{
NSLog(@"scrollview size : %@", NSStringFromCGSize(self.scrollView.frame.size));
NSLog(@"content size : %@", NSStringFromCGSize(self.scrollView.contentSize));
}
Content size should be bigger than frame size of the scrollview.
Upvotes: 1