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.