I have created a UIScrollView and added subviews to it in Interface Builder, but I am not able to get it to scroll. I have looked at other similar questions, but I have set my contentSize appropriately. In fact, I have even tried setting incredibly large contentSize and it fails to make a difference. What am I missing? Note that the UIScrollView is an IBOutlet:
@property (strong, nonatomic) IBOutlet UIScrollView *scroll;
This is in my viewDidLoad:
self.scroll.scrollEnabled = YES;
[self.scroll setContentSize: CGSizeMake(2400,8000)];
Here are screenshots from Interface Builder:
Here is the view while running, but any attempt at horizontal or vertical scrolling at any point does not work.
Try turning off autolayot by
Select
ViewController
thenfile inspecto
r and ininterface builder document
section you can seeUse Autolayout
checked or not. If its checked, thenuncheck
it.This info UIScrollView And Autolayout may give the details.
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):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.Try this code to auto set content size
first copy and paste your view controller in viewDidLoad Method
then copy and paste in below viewDidLoad Method
connect YOUR_SCROLLVIEW delegate also and TextFeild Delegate also Happy Coding!!!
If you are using auto-layout than Put everything in the UIScrollView into another UIView, and put that UIView as the only child of the UIScrollView. Then you can use AutoLayout.
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.