0
votes

I have a UIScrollView defined in my storyboard, with an outlet. This scrollview gets a UIImageView added to it through code (not on storyboard). When I run the app, the image is stretched out of aspect. As soon as I touch to zoom the image, it snaps to its proper aspect ratio.

What is going on here?!

@IBOutlet var scrollView: UIScrollView!

var imageView = UIImageView()
var selectedImage: UIImage!

override func viewDidLoad() {
    super.viewDidLoad()

    scrollView.delegate = self
    imageView.frame = CGRectMake(0, 0, scrollView.frame.size.width, scrollView.frame.size.height)
    imageView.image = selectedImage
    imageView.contentMode = .ScaleAspectFit
    imageView.userInteractionEnabled = true


    scrollView.contentMode = .ScaleAspectFit
    scrollView.addSubview(imageView)

    let scrollViewFrame = scrollView.frame
    let scaleWidth = scrollViewFrame.size.width / scrollView.contentSize.width
    let scaleHeight = scrollViewFrame.size.height / scrollView.contentSize.height
    let minScale = min(scaleHeight, scaleWidth)
    scrollView.minimumZoomScale = 0.3
    scrollView.maximumZoomScale = 1
    scrollView.zoomScale = minScale

    centerScrollViewContents()


    self.closeBtn.layer.cornerRadius = CGRectGetWidth(self.closeBtn.frame)/20.0


}

When loaded: enter image description here

After moving zoom at all: enter image description here

ALMOST FIXED: I edited the code to define the imageView.frame in viewWillLayoutSubviews instead of viewDidLoad, as was suggested in the answers below. I am now having a new issue - the image is unable to zoom in... it allows you to zoom until you release your fingers and then it snaps back to original size.

Is this to do with scrollView.maximumZoomScale not being big enough?

corrected code:

override func viewWillLayoutSubviews() {
    super.viewWillLayoutSubviews()
    imageView.frame = CGRectMake(0, 0, scrollView.frame.size.width, scrollView.frame.size.height)
}
override func viewDidLoad() {
    super.viewDidLoad()

    scrollView.delegate = self
    //imageView.frame = CGRectMake(0, 0, scrollView.frame.size.width, scrollView.frame.size.height)
    imageView.image = selectedImage
    imageView.contentMode = .ScaleAspectFit
    imageView.userInteractionEnabled = true


    scrollView.contentMode = .ScaleAspectFit
    scrollView.addSubview(imageView)

    let scrollViewFrame = scrollView.frame
    let scaleWidth = scrollViewFrame.size.width / scrollView.contentSize.width
    let scaleHeight = scrollViewFrame.size.height / scrollView.contentSize.height
    let minScale = min(scaleHeight, scaleWidth)
    scrollView.minimumZoomScale = 0.3
    scrollView.maximumZoomScale = 2.0
    scrollView.zoomScale = minScale

    centerScrollViewContents()


    self.closeBtn.layer.cornerRadius = CGRectGetWidth(self.closeBtn.frame)/20.0


}
1
can you please post a screenshot for better understandingTeja Nandamuri

1 Answers

2
votes

It's too early to set the frame in -viewDidLoad() method

if u will print your scrollView.frame in -viewDidLoad() this will actually return the frame before your view will layout it's subviews.

So u need to make constraints to imageView OR in viewWillLayout redefine imageView.frame.