I've been working on this for the past couple of hours and can't figure this out.
I have a scrollview with content added to it as a subview, but my scroll view won't scroll past the tabBarController, even though the content goes beyond this point.
self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, self.view.bounds.size.height)];
self.scrollView.contentSize = CGSizeMake(320, self.scrollView.frame.size.height);
self.scrollView.autoresizesSubviews = YES;
self.scrollView.delegate = self;
[self.view addSubview:self.scrollView];
I figured this would work, but definitely doesn't. I've been searching for an answer and can't find one, help is much appreciated!
If your scrollviews frame and contentSize are the same height, it won't be able to scroll, the contentSize should be higher than your scrollview frame in order to make the scrollerview be able to scroll.
JonLOo’s answer covers the original question. To address your new question:
How do I get the scroll view to automatically adjust how much scroll is needed based on how much content there is?
Read through Apple’s Technical Note TN2154:
UIScrollView And Autolayout, particularly the Pure Auto Layout Approach section. Essentially, the constraints on the content views must fully specify a size, which the scroll view uses as its content size.
On method: viewDidLoad you will get different height of your scrollView. Try to get on viewDidAppear
Try also:
scrollView.scrollEnabled = YES;
Related
I have created a tableview in code as follows:
_myTableView = [[UITableView alloc] initWithFrame:
CGRectMake(160, 80, 140, 100) style:UITableViewStylePlain];
_myTableView.delegate = self;
_myTableView.dataSource = self;
_myTableView.scrollEnabled = YES;
[self.view addSubview:_myTableView];
It largely works as it should with the following exception. Because the results in the table vary, I manually adjust the height of the tableview so that it only takes up as much space as the returned rows need as follows:
-(void) changeTVHeight: (float) height {
//height calculated from number of items in array returned.
CGRect newFrame = CGRectMake(120, 80, 180, height);
self.myTableView.frame = newFrame;
}
This works great for shrinking the tableview if there aren't that many results.
However, if there are a lot of results, the tableview expands below the visible part of the screen or the keyboard. In this case, I would like to be able to scroll the Tableview to see the lower rows.
scrollEnabled is set to YES.
But while it does allow one to scroll a bit, the scroll is resisted so with effort you can scroll a little bit but due to rubber band effect you cannot get further than a few rows below the screen and you cannot tap on the lower rows.
I am using autolayout in storyboard for much of the screen. The overall screen scrolls fine but this merely moves the tableview anchored to the screen up and down. There are no constraints on this tableview but it is added as a subview of the view.
My question is how can I make the tableview scrollable so that it scrolls without resistance?
Thanks in advance for any suggestions.
Edit:
I tried adding the tableView to self.scrollView instead of self.view. This anchored the tableView to the scrollview so it is possible to scroll the whole screen down and see the bottom of the tableview. However, this is not ideal because the rest of the screen is empty way down and you can't see the context for the tableview. (It's an autocomplete for a textfield at top of screen.)
In contrast when the tableview is added to self.view, it is in correct place, it semi-scrolls or bounces. It just doesn't scroll down to where I need it to scroll.
You need to set a limit so that the table view cannot be larger than the view itself. Tableviews are built on UIScrollView and will handle scrolling on their own, you don't need to try to size it manually. The reason the table view bounces but doesn't scroll is because it is extending below the bottom of the screen. It wont scroll because it has already scrolled to the bottom, you just can't see it because it's outside of the superview.
-(void) changeTVHeight: (float) height {
CGFloat limitedHeight = MIN(height, self.view.frame.size.height)
CGRect newFrame = CGRectMake(120, 80, 180, limitedHeight);
self.myTableView.frame = newFrame;
}
Currently I am having a very trivial issue with my UIScrollView that lies within a UIViewController's UIView.
At the moment, I am using a xib file because alot of the info that I am putting inside of the UIScrollView is way too much for me to code.... (ALOT OF INFO, IMAGES, blah blah blah) but their all static.
This is the code I have used to add the UIScrollView to the UIViewController's UIView:
_scrollView = [[UIScrollView alloc] init];
_scrollView.delegate = self;
_scrollView.scrollEnabled = YES;
_scrollView.showsVerticalScrollIndicator = YES;
_scrollView.contentSize = CGSizeMake(self.view.bounds.size.width, self.view.bounds.size.height * 7 );
[self.view addSubview:_scrollView];
As you can see, the UIScrollView is LARGE!!!
I set it's content size to 7 times the size of the UIView AND set the delegete method to conform to the UIViewController.
Now, I am able to scroll perfectly BUT I am unable to scroll through the entire UIScrollView's content, even though I set it's content size to A LARGE amount!!!
What the heck am I doing wrong? :(
I also commented out this:
//_scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, self.view.bounds.size.width, 2389)];
But when I use (^) line of code in comparison to the below (/) line of code.... No scrolling can be made....
_scrollView = [[UIScrollView alloc] init];
What am I doing wrong?! /sob....
Thank you!
EDIT:
The UIScrollView is now HALF THE SIZE of the UIView in xib.....Everything was setup in xib as well... this is ALL the CODE there is to be seen...
If you want some snapshots... here....
The UIScrollview in the first picture is scrolled down ALL THE WAY.....
I also NSLOG(#"self.view.bounds.size.height * 7) -> I get 2000+
Well because I cant see all of your code or its results I can only assume that the UIScrollView is larger then the screen.
This means that some of the content is hidden outside the bounds of the screen.
Hope this helps :)
It's a bad idea to place all of the views directly in the scroll view.
try putting them into one view that only it lives directly within the scroll view.
I have a scroll view, added in the IB, and also I added a lot of objects/controls to it on IB itself. Some of them are added to the scrollview but they are out of its bounds.
Back in the class, I'm setting its content size like this
self.scrollView.delegate = self;
self.scrollView.contentSize = CGSizeMake(self.view.bounds.size.width, self.view.bounds.size.height + 500);
The scrolling length I observe is not that much. I just observe like 500 px (while the height I set in the content size is around 900 px).
Please tell me what I'm doing wrong.
I use the IB to set the scroll view and the inner controls and I let the scroll view to be longer than the view area it self as why do you need to set it inside the code unless you draw the inner controls dynamically at the runtime ...I hope it is useful
So I've created an UITextView and put it inside an UIScrollView. However, I disabled scrolling for UITextView, so I was wondering is it possible for me to scroll the textview at the same time while I'm scrolling through my UIScrollView?
I'm not sure if someone has already asked something like this, I tried to search through the site and couldn't find any topics related to my problem. If there's an existing post like this I would appreciate it if someone plink me to it.
UIScrollView is a scrolling UIView. So you need just to add the UITextView to the scroll view as a subview and it will scroll with it, basically something like this:
UIScrollView *scrollView = [UIScrollView alloc]initWithFrame:CGRectMake(0,0,320,460)];//adjust the frame to fit your needs
scrollView.contentSize = CGSizeMake(400, 600);//make sure the size in contentSize is bigger than the size of the scrollView frame: (400, 600) is bigger than (320, 460), otherwise the scroll effect won't be performed
UITextView *textView = [UITextView alloc]initWithFrame:CGRectMake(0,0,200,400)];//adjust the frame to fit your needs
[scrollView addSubview:textView];//need to add the text view as a subview
[self.view addSubview:scrollView];
So a little explanations of what my view is doing before the problem, the page loads a bunch of buttons that are clickable into the contentView which is a subView of my scrollView.
i got a UIViewController, inside i have my
UIScrollView scrollView to handle the scrolling and zooming
UIView contentView which i add all the UIButtons to.
Alright i'll try to only put the code which i believe might need to be changed.
in viewDidLoad
scrollView.contentSize = CGSizeMake(320, (20+totalRows*OFFSET_Y) );
[scrollView setMaximumZoomScale:4];
[scrollView setMinimumZoomScale:1];
[scrollView setDelegate:self];
[contentView setFrame:CGRectMake(0, 0, 320, (20+totalRows*OFFSET_Y) )];
[self.scrollView addSubview:contentView];
Doing some calculation to see how big i need to content size to be to fit all the buttons, then i add the contentView as subView to my scrollView.
I then add all the UIButtons as subview of my contentView
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return [self contentView];
}
Got my delegate method for zooming returning the contentView.
Ok so everything is scrollable and zoomable like it should be..all my UIButtons are still clickable and works how it should be.
The problem is after i zoom in/out when i scroll all the way to the bottom of my scrollView it's being cut off from the bottom, so the last row of buttons are being cut in half. even when i rezoom to 1:1 it's still cut off.
I've got a NavigationController in the app which is 44 pixels, not sure if that's screwing something up somehow.
I was checking the contentSize of the scrollView and contentView and before any zooming is done, the Height of my scrollView is 44 pixels bigger than the contentView, but after any zooming the ratio is 1:1 and that seems to be the problem.
I'm not changing the size anywhere in the code though.
Any help is appreciated!
Thanks
Like i thought before I added this piece of code
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale (float)scale
{
self.scrollView.contentSize = CGSizeMake(self.scrollView.contentSize.width, self.scrollView.contentSize.height + (44 * scale));
}
And now Zooming doesn't cut anything off and seems to be working correctly.
I still don't believe this is what I should have to do but I guess it works for now.
Still hoping someone has a better answer!
After playing more with my app I realized that the zooming was never the problem to begin with. The actual problem was with my UIView *contentView. After changing the background colour of my scrollView and my contentView (to 2 very diff colours) I noticed that my contentView wasn't long enough to cover all of the buttons.
So at first load the scrollView was big enough to see everything but once you zoom and it takes the size of the contentView it wasn't being adjusted properly!
All that to say, it finally works how it should!..haha no replies but hopefully someone might find this useful! Best tip was to change the background colours.
Good Day,
I realize very late to the game, but wanted to updated what i found on this, should anyone else come across this issue.
All did was set BOTH the UIImageView and the UIScrollView to "Aspect Fit" and this fixed the issue for me. This may be an Xcode 7 feature, but it works.