Can't get UIScrollView to work - ios

This is a storyboard app. I added a UIScrollView on top of the UIView that is there in the view controller by default. Then I added some UI elements on to the scroll view. After designing the viewable part of the Scroll view, using its handles I stretched it vertically and pushed it up a bit so that that part is visible for me to design the rest of the screen. After I was done I positioned the scroll view back to fit the view. Please note that I did not resize the scroll view to match the size of the view from the IB.
Then I added all 4 constraints to the scroll view (Leading and Trailing space to superview, Top and Bottom space to superview). Then I selected the UIView, opened up the size inspector and changed the Bottom Space to superview's value to 0. That automatically resized the UIScrollView to fit inside the view controller.
Then I added the following lines of code inside the viewDidLoad method.
self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width, self.scrollView.frame.size.height);
self.scrollView.frame = self.view.frame;
But this does not work. The UIScrollView won't scroll. Yes, the scrolling is enabled.
Can anyone please tell me what I might be missing here?
Thank you.
EDIT:
I found an old project of mine here. It utilize the way I just explained above and it works! No idea why it doesn't anymore.
I also added a demo project here with the issue.

If I understood your code correctly (a screenshot of the view would help) you set the scrollviews frame to be full size of the view and set then contentSize to the view's size. A scrollview scrolls only if the contentSize is larger than its size.

This might not be the complete answer to your question, but it will help you and others for sure.
To design the views that are bigger in size than the size of the UIScrollView, the best way to have the UIView as a child view to UIScrollView and put all the content on the UIView and finally place the UIView as subview of the UISCrollView. AT last you can set the size of the UIView as the content size of the UISCrollView.
Cheers. :)

Related

UIScrollView bounces back when let go

I have a followed this answer Putting a UIScrollview in a viewcontroller (storyboard) to embed a scrollview in a viewcontroller (Mine is vertically instead of horizontally).
All the constrains are working except the fact that the view bounces back when it is let go. I have tried a lot of different solution, but none is working! Please help, I'm using autolayouts my main goal is to be able to show all the buttons on the screen through scrolling.
EDIT:
I have been trying all types of ways to increase the contentSize, but nothing happens - the scrollview won't scroll properly. I tried different codes from people with the same problem, but the bouncing won't stop. Am I doing anything wrong on my constraints? This is extremely frustrating, please help. My constraints (using autolayout) (as I said, I followed this answer Putting a UIScrollview in a viewcontroller (storyboard))
You have to increase your scrollView's contentSize to fit all of it's sub views.
You should add subview to scrollview, then add all other views to that subview.
scrollView
contentView
subview_1
subview_2
Then add constraints between scrollView and contentView (leading, trailing, top, bottom) and contentView should have width and height constraints set properly (set width equal to scrollView width)... the content size will be calculated automatically.... just set height

Objective-C UIScrollView not scrolling

I have never used UIScrollView before, so I apologize in advanced if my question seems dumb.
I add a UIScrollView to my storyboard scene and then I added labels inside my the scrollview. But when I run my app nothing scrolls. and my text runs off the app screen. Here is a screenshot of the list of items inside my storyboard scene
My question is, is there any steps I may have missed to get this working?
Set this in code:
// the size dimensions should be larger than the scroll view's frame size.
scrollView.contentSize = CGSizeMake(xSize, ySize);
Your problem is because scrollView has contentSize property which is not set yet. So it's not scrolling. To do it in code you should try out David's answer to try it in storyboard, you should add these constraints
These two constraints will make height-
scrollview vertical space to toolbar(bottom constarint)
scollView vertical space to launchImage(top constraint)
These two will make width
scrollView leading space to container/superView
scrollView trailing space to container/superView

scrollview has ambiguous content width on Xcode 6

I can't understand how can I solve this auto layout problem, i have this View:
There is an UIScrollView with the elements you see in the View, but the View gives me this warning:
scrollview has ambiguous content width
I can't understand how to solve this problem, I want the the view is for the full screen size, and I want that the width will be the screen width, how I can solve it?
I observed its fix in XCode 7.3 and it is just 10 sec work. I made a video on it. Check here:
https://www.youtube.com/watch?v=yETZKqdaPiI
All you have to do, add a subview to UIScrollView with same width and height. Then select ViewController and press Reset to suggested constraint. Please check video for clear understanding.
Thanks
This is because you are not providing enough constraints so that Xcode can find Width of your scroll view.
You can see on your screen there isn't any constraints that can be satisfied to find width of scroll view.
You can do it by providing leading and trailing space of Image view which contains fix width. It also can be done with any other views. But i will prefer control which contains fix Height & Width So there isn't require to set hugging priority or Compress resistance.
Put everything in a View and that View put inside a ScrollView.
Editor -> Embed in -> View
The solutions is:
You have a UIImageView at the top. You set the width of the UIImageView to be the same as the width of the root view of the UIViewController
You set the leading and trailing space of the UIImageView to the UIScrollView to be both 0
You set the height of the UIImageView to be the height you want the image to be. And then you set the view mode of the UIImageView to be 'Aspect fit'.
I had the same problem a month ago. Add a constraint that makes the UIView inside the UIScrollView have the same size of the UIScrollView's content width. If you have multiple views inside the UIScrollView, put your UIViews inside a new UIView and make sure the new UIView has the same content width size as the UIScrollView.
Best regards,
I too faced this issue and the only way i got it solved was using the below strategy
Add a scroll view and set its constraint to fill in the screen
Add a UIview inside the scroll view lets call this view as the content view.
In this contentView, set top, bottom, left and right margins to 0 (of course from the scrollView which is the superView);
This should do it now you can follow step 3 and continue with your UI
Place your view inside the content view and add constraints to it accordingly.
Hope it helps

Adding Scroll view using storyboard in ios

I am trying to add a scroll view using storyboards in iOS. I have done so and it is working, but the problem is when I am using a scroll view of content size more than 800 it is not working. I have gone through many tutorials and have found that it will work only when autolayout is unchecked. Can we make it work with autolayout selected? Can anyone help me with this? Thanks in advance.
In order to make a scrollview scrollable, the content size must be larger than the scrollview's frame so the scrollview has something to scroll to. Use setContentSize to adjust the content size:
[scrollview setContentSize:CGSizeMake(width, height)];
refer this link UIScrollView not scrolling in iOS7 with autolayout on
try this
add the method
- (void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
[yourScrollView setContentSize:CGSizeMake(SOME_FLOAT_VALUE, SOME_OTHER_FLOAT_VALUE)];
}
I continue to have difficulties with this also. This is what I have tried. It works well, but I do get a warning, maybe someone can comment and help us all out.
Keeping auto layout on I have done the following..
In my view controller I dropped in a scrollview and sized it to whatever size was needed, but either the size of the screen or smaller. I then set the following constraints with the pin and alignment tabs for the scrollview... Top to view, bottom to view, both sides to view, and center in container. This allows for rotation.
Then, I dropped in a view and sized it to be larger than the scrollview. I have extended both the length of the view and its width for different apps. Inside this view you put all of your subviews, (image views, buttons, whatever you need) to be contained in the scrollview. For this view I have also set the following constraints... Top to scroll, bottom to scroll, width, and center in container. Then in the left screen for the storyboard I selected the bottom constraint for the view. It should be some negative number. In the attributes inspector, I then made this bottom constraint 0.
This has worked for me in the past, I can scroll in both portrait and landscape. I have also added pinch gestures and pan gestures for the view to be able to move things around. Hope this helps.
- (void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
[yourScrollView setContentSize:CGSizeMake(SOME_FLOAT_VALUE, SOME_OTHER_FLOAT_VALUE)];
}
for ipad(IOS7)
SOME_FLOAT_VALUE=0;
SOME_OTHER_FLOAT_VALUE=1200;
This is enough for scrolling a view

UIScrollView not scrolling in ios6

In my UIViewController I have a UIScrollView and UIView. These are two different view, means UIView is not inside UIScrollView. I have different view on scrollview that i am setting programmatically and increasing contentSize. And this UIView is hidden, i am showing is sometimes. The problem is that when i hide this UIView, that UIScrollView stops scrolling.
Why this happening?
I have set all leading edges to superview of both. Does it matter?
Please guide me.
Hi all, I am sharing the image of my ViewController Scene
I am using auto layout (iOS6). Scroll View ContentSize is (768,1400) which i am setting in ViewDidAppear. The View below Scroll View is hidden and Scroll View is scrolling without any problem.
Now if i show and hide the View, the Scroll View locks, its not scrollable now.
That is problem i am facing.
I am not doing anything in code more than setting content size.
If you need any other information, please tell me.
The contentSize in iOS 6 "auto-sizes" itself with auto layout. This means you cannot set the contentSize. There are hacks that suggest setting it in the viewDidAppear call, but I would avoid this. If you read the iOS 6 release notes (search for UIScrollView on the page) it gives you a couple solutions. One of them involve the translatesAutoresizingMaskIntoConstraints property.
Remove all your code related to sizing the scrollview content size. Autolayout uses this while you are scrolling now. After you do this add the following code
// Add 200 points to the bottom of the scroll view
[self.scrollView setContentInset:UIEdgeInsetsMake(0, 0, 200, 0)];
self.scrollView.translatesAutoresizingMaskIntoConstraints= NO;
Without any code, I'll hazard a guess...
This would be typical in a case if your contentSize doesn't exceed the size of your UIScrollView. If you were to, say, hide your view, then change the size of your scroll view and your scroll view exceeds or is equal to your contentSize, then you will not have anything to scroll.
Another possibility is if you have disabled user interaction.

Resources