UIScrollView not scrolling regardless of large contentSize
Turning Auto Layout off works, but that's not the solution. If you really need Auto Layout, then use it, if you don't need it, turn it off. But that is not the correct fix for this solution.
UIScrollView
works differently with other views in Auto Layout. Here is Apple's release note on Auto Layout, I've copied the interesting bit (emphasis mine, at third bullet point):
Here are some notes regarding Auto Layout support for UIScrollView:
- In general, Auto Layout considers the top, left, bottom, and right edges of a view to be the visible edges. That is, if you pin a view to
the left edge of its superview, you’re really pinning it to the
minimum x-value of the superview’s bounds. Changing the bounds origin
of the superview does not change the position of the view.- The UIScrollView class scrolls its content by changing the origin of its bounds. To make this work with Auto Layout, the top, left, bottom,
and right edges within a scroll view now mean the edges of its content
view.- The constraints on the subviews of the scroll view must result in a size to fill, which is then interpreted as the content size of the
scroll view. (This should not be confused with the
intrinsicContentSize method used for Auto Layout.) To size the scroll
view’s frame with Auto Layout, constraints must either be explicit
regarding the width and height of the scroll view, or the edges of the
scroll view must be tied to views outside of its subtree.- Note that you can make a subview of the scroll view appear to float (not scroll) over the other scrolling content by creating constraints
between the view and a view outside the scroll view’s subtree, such as
the scroll view’s superview.
Apple then goes on to show example of how to correctly use UIScrollView
with Auto Layout.
As a general rule, one of the easiest fix is to create a constraint between the element to the bottom of the UIScrollView. So in the element that you want to be at the bottom of the UIScrollView, create this bottom space constraint:
Once again, if you do not want to use Auto Layout, then turn it off. You can then set the contentSize
the usual way. But what you should understand is that this is an intended behaviour of Auto Layout.
UIScrollView not scrolling
It's always good to show a complete working code snippet:
// in viewDidLoad (if using Autolayout check note below):
UIScrollView *myScrollView;
UIView *contentView;
// scrollview won't scroll unless content size explicitly set
[myScrollView addSubview:contentView];//if the contentView is not already inside your scrollview in your xib/StoryBoard doc
myScrollView.contentSize = contentView.frame.size; //sets ScrollView content size
Swift 4.0
let myScrollView
let contentView
// scrollview won't scroll unless content size explicitly set
myScrollView.addSubview(contentView)//if the contentView is not already inside your scrollview in your xib/StoryBoard doc
myScrollView.contentSize = contentView.frame.size //sets ScrollView content size
I have not found a way to set contentSize in IB (as of Xcode 5.0).
Note:
If you are using Autolayout the best place to put this code is inside the -(void)viewDidLayoutSubviews
method .
UIScrollView not scrolling no matter what I try
I'm surprised Interface Builder isn't showing a Constraints error or warning...
When using auto-layout, the contents of the scroll view define the contentSize
. That means the contents (in your case, a single UIView
holding 3 smaller UIView
s) must have a constraint to the Top, Leading, Trailing and Bottom of its superview (the scroll view)... and it must have a Height and Width.
In your storyboard, delete the "Center Vertically" constraint, and add a Height constraint (of 1000) to the "content" view. Then delete your scrollView.contentSize = ...
line from code. Run the app, and you should have no problem scrolling.
UIScrollView Not Scrolling?
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.
UIScrollView not scrolling although contentSize is smaller than UIImageView
That's because that your size of bounds of scroll view is larger than your content size.
The content size need to be the actual size of image view, and the visible size is set by frame / bounds. I guess you want to scroll a image in a {200, 200} sized rect?
Try this:
UIScrollView * imgScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)]; // this is your visible rect
[imgScrollView setScrollEnabled:YES];
[imgScrollView setClipsToBounds:YES];
[imgScrollView addSubview:imgView];
[imgScrollView setBackgroundColor:[UIColor yellowColor]];
[imgScrollView setContentSize:imgFrame.size]; // this is your image view size
UIScrollView contentSize not working
I had the same problem. Auto Layout for UIScrollView
is messed up.
Work around: Put everything in the UIScrollView
into another UIView
, and put that UIView
as the only child of the UIScrollView
. Then you can use Auto Layout.
If things near the end is messed up (the end of whichever direction your UIScrollView
scrolls), change the constraint at the end to have the lowest possible priority.
UIScrollView not scrolling at all
If you are using paging then use
self.scrollV.contentSize = CGSizeMake(self.scrollV.frame.size.width *kNumberOfPages, 220);
self.scrollV.showsHorizontalScrollIndicator = NO;
self.scrollV.showsVerticalScrollIndicator = NO;
self.scrollV.scrollsToTop = NO;
self.scrollV.delegate = self;
Here you can use number of pages to show.But before this please set delegate UIScrollViewDelegate
of UIScroll view to that class. Please also check autolayout in your view too.
Hope this helps.
UIScrollView not scrolling to bottom in iOS 7
So I figured out a pretty unsatisfying solution rather quickly by wrapping the call in an async dispatch block.
// Scroll to bottom.
CGPoint bottomOffset = CGPointMake(0, self.contentView.contentSize.height
- self.contentView.bounds.size.height);
if (bottomOffset.y >= 0.0)
{
dispatch_async(dispatch_get_main_queue(), ^{
[self.contentView setContentOffset:bottomOffset animated:YES];
});
}
If anyone understands what is really causing the problem and can provide a better solution I'd gladly accept that as the answer, but for everyone else dealing with the same issue hopefully this works for you as well.
Related Topics
Swrevealviewcontroller - Manually Switch to Another View
Header Displaced in Tableview with Uirefreshcontrol
Swiftui in iOS14 Keyboard Avoidance Issues and Ignoressafearea Modifier Issues
Swrevealviewcontroller Close Rear View When Tapping Front View
Debugging Mobile Safari in iOS 8 and iOS 9
Info.Plist File for React Native iOS App Using Expo Sdk
How to Get Selected Text from Uitextfield in Iphone
How to Loop Video with Avplayerlooper
Will Apps That Use Telprompt Be Rejected
Is -[Uitableview Reloaddata] Asynchronous or Synchronous
iPhone - Didselectrowatindexpath: Only Being Called After Long Press on Custom Cell
Could Not Attach to Pid:"####" Unable to Attach
Info.Plist Utility Error: "Info.Plist Couldn't Be Opened Because There Is No Such File"
Multiple Checkmark When Row Selected in Uitableview iOS