In IB I have my UIView. Then I have a sub-UIView with a UIScrollView as a sub view. Then the UIScrollView has a sub-UIImageView. The UIScrollView and UIImageView are the same size. They're much bigger than the UIView of which they are subviews. I assumed this would make scrolling work. It doesn't. Is some sort of code required for scroll views to work?
问题:
回答1:
You need to set UIScrollView.contentSize
to match the total scrollable size, which is your subview frame size in this case.
回答2:
As mentioned in the accepted answer, you must set the UIScrollView
's contentSize
property.
This can be done in Interface Builder.
- Select the scroll view
- Select the 'identity inspect' in Utilities pane on the right
- Under 'User Defined Runtime Attributes' click the '+' button
- Set the 'Key Path' value to 'contentSize'
- Set the 'Type' value to 'Size'
- Set the 'Value' value to '{width, height}' (eg: '{320, 600}')
Build and run and your scroll view will scroll.
The content inset does not affect scrolling. See What's the UIScrollView contentInset property for?
回答3:
To scroll, you have to make the scrollview's frame smaller than its content, the contained image or view.
回答4:
This might be obvious to most, but I spent ages wondering why my UIScrollView wouldn't scroll so I'm posting what was stopping me in case it helps anyone else:
The UIScrollView has to be of the dimensions of the visible area in which you wish it to be presented and not the size of it's contents.
Ridiculous on my behalf I know, but just in case it helps someone.
回答5:
I placed all the content of my scrollview in IB. (buttons, labels, text fields, etc). The full size is 500 tall. I then resized it to 436 tall in IB.
Then in code, I put this is viewDidLoad:
optionsScrollView.contentSize = CGSizeMake(320,500);
So that leaves 64 pixels that I can scroll. It works perfectly.
I also placed "UIScrollViewDelegate" in the <> braces of @interface for my .h file and tied the delegate outlet of the scrollview to File's owner in IB.
回答6:
more, did you enable scrolling? look at the property
@property(nonatomic, getter=isScrollEnabled) BOOL scrollEnabled
回答7:
The other important thing that I don't see mentioned here is that UIScrollView does not play nicely with AutoLayout. If it seems like you've done everything correctly, check if your ViewController has autolayout turned on and, if so, turn it off.
(Every time you scroll, the views are re-laid-out. Gak!)
So:
- Make sure scrollview's
contentSize
is bigger than itsframe.size
- Make sure
AutoLayout
for the ViewController is turned off.
回答8:
I could solve the scrolling problem with the following answer:
https://stackoverflow.com/a/39945124/5056173
By me the trick was:
- You now need to set the height of the content UIView. You can then either specify the height of the content view (blech) or use the height of the controls contained within by making sure the bottom control is constrained to the bottom of the content view.
I have set the height and width of the view inside the scrollView with center vertical and horizontal alignment and that was the reason, why it did not work!
After deleting this constraints, I need to add equal width (scrollView and the view inside the scrollView) AND I set the height of the view inside the scrollView directly with the content. Which means: The last element in the view must have a bottom constraint to the view!!
回答9:
Scroll view works with this:
Frame
then
contentSize
views or objects etc...
If your frame is set to your content size then it won't scroll.
So set your frame ( in IB right panel -> second last tab 'Size Inspector") to the length of your app ( in my case it is 367 as i have a navbar and a tab bar) then programatically set the contentSize to - yup you guessed it ... more than your frame so it can scroll.
Happy days!!