Adding Scroll view using storyboard in ios - 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

Related

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

UIScrollView using Autolayout will only bounce

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;

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.

Cannot Scroll UIScrollView

I've got a UIScrollView in IB linked to the delegate and with the proper referencing outlet, declared in my header:
IBOutlet UIScrollView *scrollView;
And called in my viewDidLoad:
- (void)viewDidLoad
{
[super viewDidLoad];
[scrollView setScrollEnabled:YES];
[scrollView setContentSize:CGSizeMake(320, 500)];
}
It has the correct size and width in IB, and the default values in the Attributes inspector.
Why can't I scroll it?
EDIT: Also, layer wise there is no other view conflicting for a z-index.
Is the content size larger than the scrollview's frame? That's the only way it'll scroll.
Double-check that you have content size bigger than scroll view size itself. If it will be bigger it will scroll.
m using Xcode 6/iOS 8+ now with auto-layout turned on, and I had the same problem in the first place, removing auto-layout doesn't work at all, so in order to make scroll view scrollable vertically, I made sure the following:
The scroll view content size height MUST be bigger than the screen
height, this almost goes without saying...
The top/bottom/left/right constraint of the scroll view HAS TO BE
pinned, I did this using storyboard so no code showing up here
If you want to make the scroll view scrollable horizontally, then make sure its content size width is bigger than the screen width, the other rule applies the same.
This worked for me, hope it can help someone.
enable scrolling
check "Scolling Enabled" and make sure the content size is larger than the screen height
scrollView.contentSize = CGSize(width: 375, height: <content height>)
I'm not sure what the answer to your question was when you asked.
For now, under AutoLayout, setting constraints for scroll view just solve your problem.

Resources