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)];
Related
I have an UIViewcontroller. I have a smaller UIVIew in this UIVIewController. Now i am trying to add an UIScrollView programmatically that should contain the smaller UIView so i can scroll the smaller view up and down. The smaller UIView is already created using the interface builder of xcode. So now i use the following code to add the scrollView and then add the smaller View in the scrollView. The scrollView should have the same size in width as the smaller View.
self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(self.smallerView.frame.origin.x, self.smallerView.frame.origin.y, self.smallerView.frame.size.width, self.smallerView.frame.size.height)];
[self.scrollView setContentSize:CGSizeMake(self.smallerView.bounds.size.width, self.smallerView.bounds.size.height*1.2)];
[self.scrollView addSubview:self.smallerView];
[self.view addSubview:self.scrollView];
The weird thing is that the objects that are contained in the smaller UIView are not inside the bounds of the UIScrollView. What am i missing? Do i have to add constraints for the smaller UIView programmatically? Any help appreciated.
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 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 have the following ViewController:
It contains two UILabels at top, an UIImageView, below it a UITextView and below this a UIButton. I have arranged them using the Interface Builder following the blue line. All of this controls are inside a UIScrollView:
[self.scrollView setContentSize:CGSizeMake(320, 660)];
[self.scrollView addSubview:self.descriptionText];
[self.scrollView addSubview:self.descriptionImage];
[self.scrollView addSubview:self.titleLabel];
[self.scrollView addSubview:self.feedNameLabel];
[self.scrollView addSubview:self.load];
So when enabling Autolayout option, I just selected the ViewControler and then "Reset to Suggested Constraints in Description View Controller". But when I run the app, the scroll still appears for the entire page, but the only control scrolling is the UIButton. When scrolling up it will scroll below the UITextView.
I have made the UITextView to resize depending on the text, so I want my UIButton to always have the same distance to the UITextView. For that I have also set Vertical Spacing to the UIButton, but like this I don't have any scroll to my page.
Using the Autolayout for the first time, can I get some suggestions on what am I doing wrong ?
edit:
- (void)viewDidLoad
{
[super viewDidLoad];
UIView *contentView = [[UIView alloc]initWithFrame:CGRectMake(0,0,320,660)];
[contentView addSubview:self.descriptionText];
[contentView addSubview:self.descriptionImage];
[contentView addSubview:self.titleLabel];
[contentView addSubview:self.feedNameLabel];
[contentView addSubview:self.load];
contentView.translatesAutoresizingMaskIntoConstraints = YES;
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 660)];
//[scrollView setContentSize:CGSizeMake(320, 660)];
[scrollView addSubview:contentView];
[self.view addSubview:scrollView];
// to resize UITextField
[self.descriptionText sizeToFit];
[self.descriptionText layoutIfNeeded];
self.descriptionText.scrollEnabled = NO;
}
Autolayout is a bit tricky when it comes to UIScrollView subviews. I would recommend:
1-Embed all your controls (descriptionText + descriptionImage + titleLabel + feedNameLabel + load) into a UIView first, say contentView:
UIView *contentView = [[UIView alloc]initWithFrame:CGRectMake(0,0,320,660)];
//add all controls as subviews to contenView
2-Add contentView as subview of self.scrollView.
3-Keep the translatesAutoresizingMaskIntoConstraints property of contentView to YES.
I recommend you read this technical note from Apple here.
If you are using AutoLayout you don't have to set the content size of your scroll view. In fact, I believe it has no effect at all. It is set automatically from the constraints you are setting up. The trick is that you have to have at least one constraint related to every side of the scroll view, otherwise the content size will be wrong and it won't scroll. So for example, if you would have a really large image in it you would need 4 constraints connecting the sides of the UIImageView to the sides of the UIScrollView. You can read about this more here.
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.