Reputation: 1557
I have UIView inside of a UIScrollView and have connected them from Interface Builder:
@IBOutlet weak var articleScroll: UIScrollView!
@IBOutlet weak var articleSubview: UIView!
I have dynamic content going into the UIView and I want its height to be drawn as large as the content (so that it scrolls). I thought that using .last to find origin.y in combination with the height of the content would do it:
var scroll = self.articleSubview
var lastItem = scroll.subviews.last as! UIView
var lastOriginY = lastItem.frame.origin.y
var lastHeight = lastItem.frame.size.height
var finalHeight = lastOriginY+lastHeight
scroll.frame = CGRectMake(0 , 0, lastItem.frame.size.width, finalHeight)
The build succeeded but nothing scrolled. I tried adding:
articleScroll.userInteractionEnabled = true
But it didn't make a difference. What am I doing wrong?
UPDATE:
It has been solved. The actual scroll view has to be resized, not the child view. Do not use the scroll.frame, instead "contentSize" on the UIScrollView was the way to go.:
self.articleScroll.contentSize = CGSizeMake(lastItem.frame.size.width, finalHeight)
Upvotes: 2
Views: 9935
Reputation: 535989
The scrollability (is that a word?) of a UIScrollView depends upon its contentSize
in relation to its bounds size. The idea is to make the contentSize
embrace the content; if it is bigger than the scroll view's bounds size, the user can scroll.
Your code doesn't set the contentSize
anywhere. It needs to!
Upvotes: 3
Reputation: 934
Resizing your scroll, scroll view have a method called scroll.scrollRectToVisible(<rect: CGRect>, animated: <Bool>)
to scroll to the frame.
Upvotes: 0