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.
Related
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
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;
I have a UIScrollView created in interface builder with a bunch of UITextView and UIImageView objects already added. I have it connected to an IBOutlet, and under -(void)viewDidLoad I set its content size to 1200x2000. User interaction Enabled, Multitouch, Scrolling Enabled and shows vertical and horizontal scroll indicators are all set to YES. When I launch the app in the iOS simulator, it doesn't scroll. What could possibly be the cause of this behavior?
viewDidLoad is to soon. You must wait until after the layout of the views has taken place. Try setting the contentSize in viewDidAppear.
Are you using autolayout? If so, check out the following answer (and forget 1.):
UIScrollView doesn't use autolayout constraints.
The best way I know how to overcome this problem without resorting to writing code is that it all can be done within a storyboard and here is how:
Leave autolayout enabled.
Make a generic UIView a subview of the UIScrollView. 2.1)Put all your View components inside this generic UIView.
Make a autolayout constraint between a subview and the UIScrollView. You must pin the right and/or bottom of the last subview to the right and/or bottom of the UIScrollView. This is how the UIScrollView knows the content size.
viewDidLoad is indeed too soon, but viewDidAppear:animated might not be late enough either. The ideal place to set the contentOffset is inside viewDidLayoutSubviews.
If you're using Autolayout, make sure you put the code to set Content size within
viewDidLayoutSubviews
I use a custom view on the UIScrollView, for pull to refresh function, and the scroll view is not scrollable. I try set contentSize with AutoLayout, and try set it by codes too. The scroll view still cannot scroll.
After spending 3 ~ 4 hours, I solve it by contentInset. It will tell scroll view to add some extra space for scrolling.
override func viewDidLayoutSubviews() {
// Add extra 15pt on the `top` of scrollView.
//self.scrollView.contentInset = UIEdgeInsetsMake(15, 0, 0, 0)
// Add extra 2pt on the `bottom` of scrollView, let it be scrollable. And the UI is more beautiful in my case.
self.scrollView.contentInset = UIEdgeInsetsMake(0, 0, 2, 0)
}
Reference docs:
Understanding the contentOffset and contentInset properties of the UIScrollView class #blog
What's the UIScrollView contentInset property for? #Stackoverflow
This is a good walk-through of scroll-views in case anyone forgot after not using them for a while: https://medium.com/#pradeep_chauhan/how-to-configure-a-uiscrollview-with-auto-layout-in-interface-builder-218dcb4022d7.
Basically, make sure you have view -> scrollview -> view, like so:
then,
1. Set scroll view constraint (top, bottom, leading and trailing) to (0,0,0,0).
2. Set inner view constraint (top, bottom, leading and trailing) to (0,0,0,0).
3. Set inner view to have equal width and equal height with parent view.
4. Select height constraint of inner view and set priority to low (250).
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.
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.