Swift: how to set UIScrollView contentSize height to content height?

I have a UIScrollView. Right now I'm just setting it to double the height of the screen (the frame in this case is just UIScreen.mainScreen().bounds ):

 class VenueDetailView: UIScrollView { required init?(coder aDecoder: NSCoder) { fatalError("Storyboard makes me sad.") } override init(frame: CGRect) { super.init(frame: frame) contentSize = CGSize(width: frame.width, height: frame.height*2) <---------------- backgroundColor = UIColor.greenColor() } func addBannerImage(imageUrl: NSURL) { let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: UIScreen.mainScreen().bounds.width, height: 300)) // TODO: Make this asynchronous // Nice to have: cache the image if let data = NSData(contentsOfURL: imageUrl) { imageView.image = UIImage(data: data) } addSubview(imageView) } } 

However, I just want it to be the size of all the contents inside it. How can I do it? Does this mean that I have to set contentSize after adding all subzones?

+5
source share
1 answer

Does this mean that I have to set contentSize after adding all subzones?

I guess, yes. Your goal should be to make the scroll view small and large in size. This is what allows you to scroll through the scroll: its contentSize larger than its own border size. So, adjust the scroll view to have a frame that matches the bounds its supervisor, but then set its contentSize to cover all of its subzones.

+9
source

Source: https://habr.com/ru/post/1235969/


All Articles