简体   繁体   中英

UIImageView in ScrollView aspect ratio not working

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: 在此处输入图片说明

After moving zoom at all: 在此处输入图片说明

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


}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM