UIScrollView using Autolayout will only bounce - ios

I am really struggling to get a UIScrollView to work correctly with Autolayout. Instead of scrolling down as it should, it just bounces, so if I drag it to see more as soon as I let go it returns to its original position.
I have my scene set up in the following way:
-Main View
- Scroll View
- Content View
- Label
- Label
The View Controller has its size metric set to Freeform. The Main View, Scroll View and Content View all have their Height set to 700, so I can see the layout correctly.
In my .h file I have an outlet connected to the UIScrollView and in my viewDidLoad method I am doing the following:
Scroller.translatesAutoresizingMaskIntoConstraints = YES;
[self.Scroller setContentSize:CGSizeMake(320, 1000)];
I've tried setting all sorts of constraints, on various things. Normally when I add a new one Xcode then grumbles that the constraints are incorrect and prompts me to update them. I have tried so many different variations, I can't remember them all but here are a few:
Pinning the height of the Scroll View
Pinning the bottom of the Content View to the Scroll View - Getting UIScrollView to work with AutoLayout
Pinning the height of the Content View
Other things I have tried:
Reading & following Apple's Technote: - https://developer.apple.com/library/ios/technotes/tn2154/_index.html
Setting the ScrollView's Content Inset - Confusion Regarding UIScrollView and AutoLayout
Setting the Content Size: UIWebView's UIScrollView subview can scroll but bounce back to the frame without appearing vertical scroll indicator
I'm sure this must be relatively straight forward, but I have been tearing my hair out. There are many similar questions on Stackoverflow and Google, but none of them seem to fully meet my requirements.
Any help or resources would be greatly appreciated.
JA

One possible reason for this is that scroll view's contentSize is reset after layout event which happens after viewDidLoad (for example due to incorrect constrains settings).

I had to put the code in viewDidAppear for it to work right
CGSize size = CGSizeMake(320, 931);
self.scroller.scrollEnabled = YES;
self.scroller.contentSize = size;

Related

UIScrollView not scroll auto layout issues

I never used auto layouts in my life and it was great time, now storyboard take off my brain.
The conception maybe is not bad. But it's still buggy and worst to use. I have worked for today and spend 8 hours to setup one scroll view and few subviews. Does it save time???
If Apple wants to support adoptive design. Why they don't use Edge Reflow conception. It is for web, but it's great and without bugs....
I have scroll view with all needed constraints, it means that the scroll view size will be the same as superview.
I am trying to change scroll view content size in -viewDidLoad method:
[self.theScrollView setContentSize:CGSizeMake(self.view.frame.size.width, 20000)];
but seems it want work.
Print out says that the content size is 320 20000, but I can scroll it.
With autolayout, you have to do things bottom-up and implicitly; in other words, instead of telling the scrollView how big its content is, you let the scrollView adjust itself based on the size of its content. This allows the autolayout to resolve the constraints between subviews and propagate the changes upward.
If you want to explicitly set the content size, you could create an additional subview containing all the content of the scrollView, and then override its intrinsicContentSize method to explicitly tell the scrollView how big it is.
Working tried out.
if you are not using autolayout then put it only viewDidLoad():
[_mainScroll setContentSize:CGSizeMake(320, 2000)];
or if you are using autolayout then follow this:
First give constrains to scrollview top,bottom,leading and trailing.
Now take one more view and give same constrains as scrollview given by you earlier.
Don't forgot to give additional constrain for that view fix height and width equal to main parent view.
or
Just watch this.
https://www.youtube.com/watch?v=rjTS9fyWqdg

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

Can't get UIScrollView to work

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. :)

Setting up UIScrollview and autolayout

I created a UIScrollview and added a few UIElements to it, it works without autolayout no problem, scrolls etc. However its good practice to use autolayout in iOS7. Now the scrollview does not scroll. How do I accomodate for this?
If I turn auto layout off, there is extra space added to the top of the uiscrollview in ios7.
I used to set up the scrollview by 'unchecking' autolayout and implementing the following code.
//to set up the scrollview
[self.scrollView setScrollEnabled:YES];
[self.scrollView setContentSize:CGSizeMake(320, 800)];
which worked. But using autolayout prevents scrolling. Where am I going wrong?
I found the right way to do it. Put a scroll view in the view controller, set x and y 0, make height bigger than screens view, e.g 1500. Place a view in the scrollview, set its x and y to 0, set its height a little less than that of the scrollview, eg 1200. Then pin this view to the scrollview. The button for pinning is and is found in the storyboard. This worked for me like a charm. The scrollview does not need to have its coordinates set in the .m file.
This worked.

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