'Change UIScrollView's frame programatically (Swift 5)

How can I change the height of the frame based on the size of the subview programatically?

scrollView.frame = CGRect(origin: scrollView.frame.origin, size: CGSize(width: scrollView.frame.width, height: scrollView.frame.height))

This is the code that i wrote to change the height of the frame, however the frame does not change even though I set the height to 1000. In addition, I have also tried to set the height of the scollView.contentSize. It only allows the subview to be able to scroll within the scrollView and it doesn't change the height.

This is how i write the code:

Image

The outcome:

Image

The full image to be displayed:

Image



Solution 1:[1]

First, please do not post pictures of your code, post the code itself. This helps to copy/paste parts of it, in order to understand your problem.

In your special case, there are just some minor issues:

  • Line 21 (no code because there is just image): This statement is completely redundant. You set the frame of the scroll view to exactly the frame of the scrollview. You can just skip this.

  • Line 23: You set the size of the image view to the size of the scroll view. What you really want is the size of the image, because you want to be able to see the complete image (by scrolling, if it's to large)

  • Missing: You also need to set the content size of the scroll view, to make it realize how large it's inner content is (e.g. to determine how much horizontal and vertical scrolling it should support)

Hence:

override func viewDidLoad() {
    super.viewDidLoad()
    
    guard let img = UIImage(named: "komtar") else { return }
    
    let imgViewFrame = CGRect(origin: CGPoint(x: 0, y: 0), size: img.size)
    let imgView = UIImageView(frame: imgViewFrame)
    imgView.image = img
    scrollView.contentSize = img.size
    scrollView.addSubview(imgView)
}

Update

If you want to modify the height of the scroll view itself, you need

  • A constraint for the height
  • An outlet connection from the constraint to your view controller
  • Modify the constraint's value property in order to change the height

But I think a scroll view is not the best approch to this. Maybe you could check this: Swipe back and forth through array of images Swift - maybe a litte old, but might give some hints to you.

Solution 2:[2]

Very thanks to @AndreasOetjen for the hints and I have solved my problem.

How I Solved

  1. Create a IBOutlet connection for ScrollView HeightConstraint
  2. Set the HeightConstraint constant to the height of the image. scrollViewHeightConstraint.constant = imageView.intrinsicContentSize.height

I think this is the only way to resize the frame of the Scroll View.

The reason i use imageView.intrinsicContentSize.height instead of image.size.height is because I referred to this so that I can remain the width ratio and increase/decrease the height based on the image height.

Outcome

Image

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 CHEE CHUAN LOH