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.
Related
I'm currently trying to put images in a UIScrollView.
I checked a lot for an answer, but none of the topics in stackoverflow works in my code. I guess I just forgot a detail in it, but can't figure it out.
So, I have in a ViewController.m this code:
- (void)initTransportData
{
_transportsData = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 654, 414, 82)];
CGRect viewSize = _transportsData.bounds;
UIImageView *imgView = [[UIImageView alloc] initWithFrame:viewSize];
[imgView setImage:[UIImage imageNamed:#"image.png"]];
[_transportsData addSubview:imgView];
}
My _transportData is defined in my ViewController.h like that:
#property (strong, nonatomic) IBOutlet UIScrollView *transportsData;
When i test it, the scrollView display a black color (i set it in my main.storyboard).
I also have a MapView above my scrollView (don't know if it can help to understand).
PS: I call my intTransportData function below [super viewDidLoad] in the viewDidLoad function.
Does anyone know what is wrong?
Thanks
You have set frame like (0, 654, 414, 82) that means your y position is 654 of scrollview. Then it is below the screen height.
So , set frame properly.
You need to add that scrollview to main view (self.view).
If you have given outlet then no need to set frame again.
You should use autolayout to manage this things easily.
Your scenario should be like, UIScrollview - UIView - UIImageView and you need to set proper constraint to every views. If you unaware of auto layout then search tutorials on google and first learn it.
Hope this will help :)
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;
I know there are tens of similar question about scrollview but I have tried everything, Scrollview is not scrolling.
First I created a uiviewcontroller with a xib file, and set it to freeform and arrange the size of it and added a scrollview on view.
Then added it as a childviewcontroller to another view controller.
-(void)scrollViewSetup
{
if (!_scrollView) {
_scrollView=[[UIView alloc] initWithFrame:CGRectMake(0,200,self.scrollView.frame.size.width,200)];
[self.view addSubview:_scrollView];
ScrollView *displayPortf=[[ScrollView alloc] init];
//add as a child view controller here
//displayPortf.delegate=(id)self;
[_scrollView addSubview:displayPortf.view];
[displayPortf didMoveToParentViewController:self];
[self addChildViewController:displayPortf];
}
}
Content size of scroll view is (768,200) on iPad.
I tried setting content size to (1920,200) in viewDidLoad didn't work then viewDidAppear didn't work then viewDidLayoutSubviews didn't work.
Then I checked the use auto layout box and tried to add constants by goingeditor->resolve auto layout issues->add constraints
How can I make scrollview to scroll?
Note: Parent view uses auto layout and storyboard.
I guess that the reason of issue is zero width of scrollView.
When you call this:
_scrollView=[[UIView alloc] initWithFrame:CGRectMake(0,200,self.scrollView.frame.size.width,200)];
_scrollView is initiated with width self.scrollView.frame.size.width, but at this moment self.scrollView is nil as it related to instance variable _scrollView that hasn't initiated yet.
Try to set some non zero float value for view width:
_scrollView=[[UIView alloc] initWithFrame:CGRectMake(0,200,768,200)];
I am trying to achieve something similar to the photo options bar in VSCO Cam. I tried setting it up with an image, just to get an idea on how to do it, but its not working.
I think one of the problems could be with the fact that the UIScrollView should be horizontal.
Here is my code:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self.scrollView setContentSize:CGSizeMake(646, 73)];
self.scrollView.scrollEnabled = YES;
UIImage *bleh = [UIImage imageNamed:#"Digits 2 1"];
UIImageView *raaaa = [[UIImageView alloc]initWithImage:bleh];
[self.scrollView addSubview:raaaa];
}
So the first thing you need to understand is the contentsize property.
The subviews inside the scrollview are its "content". They can be arranged in any way you want inside the view.
In order to make it scroll though, you need to let the scrollview know how much "space" its content is taking up.
In your case, I'm going to guess that your image is 646 x 73 in dimensions. If you set the contentsize to that, the scrollview doesn't need to scroll because it can show all the content at once. However when you increase it, the scrollview now knows there is more content outside of the screen and it will allow you to scroll.
The second thing you need to understand is the position of the subviews. If you set the frame of your imageview to (0,0,646,73), it will be in the "left-most" position inside the scrollview. Thus, it will only scroll to the right (the white space you see).
Try this code and see if you can understand whats happening:
[self.scrollView setContentSize:CGSizeMake(1292, 73)];
UIImage *im = [UIImage imageNamed:#"Digits 2 1"];
UIImageView *v1 = [[UIImageView alloc]initWithImage:im];
UIImageView *v2 = [[UIImageView alloc]initWithImage:im];
v1.frame = CGRectMake(0,0,646,73);
v2.frame = CGRectMake(646,0,646,73);
[self.scrollView addSubView:v1];
[self.scrollView addSubView:v2];
Using Storyboards, I have a UITabBarController with a UIScrollView inside, with a UIView inside the scrollView. My view is very large, and does not completely fit in the storyboard. I am not able to scroll all the way to the bottom of the View.
If you was building all of this from scratch, how would you go about it?
Say you have a UITabBarController with 2 views. hence you have two View Controllers. Lets call them 'A' and 'B'. Configuring two views within a tab bar controller shouldn't be too difficult if you refer to this link. You can achieve this using Storyboard too.
Now say, in view 'A' you want your large UIView within the UIScrollView. What you do is declare a UIScrollView with a framesize that is equal to the screen's dimensions but set the contentSize to your large size that you wanted. You can now have you large view within this scroll view.
Example:
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 520)]; // little less than 568 to accommodate the tabBar
[scrollView setContentSize:CGSizeMake(320, 1500)];
UIView *largeView = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 320, 100)];
[scrollView addSubview:largeView];
[self.view addSubview:scrollView];
You can do the above in Storyboard as well but i prefer using code for such tasks.
Hope that helped. Let me know if you need more help.
Did you set contentSize for your scroll view? You should to set contentSize for scroll view to size of content view. For example if you want to show view in scrollView you should do something like this:
[scrollView addSubview:view]
scrollView.contentSize == view.bounds.size
I was able to figure it out. What I ended up doing was creating a new Storyboard with a tabView already implemented. Added a new scrollView to the storyboard tabView then copied my original UIView, that I wanted to scroll in, into the scrollView.
I draged the new UIScrollView out into my .h file and created a new property.
#property (strong, nonatomic) IBOutlet UIScrollView *scrollView;
Then I set the new property up like so and viola!
[super viewDidLoad];
[self.scrollView setScrollEnabled:YES];
[self.scrollView setContentSize:CGSizeMake(320, 800)];