iOS 10 Issue: UIScrollView Not Scrolling, Even When ContentSize Is Set
OK. I solved it.
I remove every single auto layout constraint for the internal (scrolled) view at build time.
I assume that iOS 10 is finally honoring the contract by forcing the top of the scrolled view to attach to the top of the scroller, even though the user wants to move it.
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 won't scroll even when I set content size
is userInteractionEnabled property equal to TRUE? Perhaps you have a subview over your scrollview that is stealing the touches?
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.
iOS - UIScrollView is not working (it doesn't scroll at all - the image stays fixed)
I just have done the same task..
Try this one.....
scrollView.delegate = self;
scrollView.scrollEnabled = YES;
int scrollWidth = 120;
scrollView.contentSize = CGSizeMake(scrollWidth,80);
int xOffset = 0;
imageView.image = [UIImage imageNamed:[imagesName objectAtIndex:0]];
for(int index=0; index < [imagesName count]; index++)
{
UIImageView *img = [[UIImageView alloc] init];
img.bounds = CGRectMake(10, 10, 50, 50);
img.frame = CGRectMake(5+xOffset, 0, 50, 50);
NSLog(@"image: %@",[imagesName objectAtIndex:index]);
img.image = [UIImage imageNamed:[imagesName objectAtIndex:index]];
[images insertObject:img atIndex:index];
scrollView.contentSize = CGSizeMake(scrollWidth+xOffset,110);
[scrollView addSubview:[images objectAtIndex:index]];
xOffset += 70;
}
Also set this one....
imagesName = [[NSArray alloc]initWithObjects:@"image1.jpg",@"image2.jpg",@"image3.jpg",@"image4.jpg",@"image5.jpg",@"image6.png",@"image7.png",@"image9.png",nil];
images = [[NSMutableArray alloc]init];
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.
Related Topics
How to Create a Pfquerytableviewcontroller with Parse in Swift
How to Fill a Circle Color by Percentage Value
Swift Select All Photos from Specific Photos Album
Audiokit - How to Get Real Time Floatchanneldata from Microphone
Saving Audio After Effect in iOS
Reload View When @Published Is Changed
Unusernotificationcenter.Current().Getdeliverednotifications Only Ever Returns an Empty Array
Swift Spritekit Playing Audio in the Background
Get Center Coordinates from Mapkit and Display in Uilabel
How to Add a Ibaction to a Button Programmatically in Swift 4
Replaykit Startrecording Sometimes Never Enters Completion Handler
Apple Push Notifications in Tvos
Uitableview Inside Uitableviewcell with Dynamic Height
iOS Swift Nsmutabledata Has No Member Appendstring
Giving Properties of a Uibutton to a Skspritenode in Spritekit