简体   繁体   English

ScrollView宽高比中的UIImageView不起作用

[英]UIImageView in ScrollView aspect ratio not working

I have a UIScrollView defined in my storyboard, with an outlet. 我在情节提要中定义了一个UIScrollView,并带有一个插座。 This scrollview gets a UIImageView added to it through code (not on storyboard). 此滚动视图通过代码(不在情节提要上)将UIImageView添加到了它。 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. 修正的问题:如以下答案所示,我编辑了代码以在viewWillLayoutSubviews中定义imageView.frame而不是viewDidLoad。 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? 这与scrollView.maximumZoomScale不够大有关吗?

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 在-viewDidLoad()方法中设置框架为时过早

if u will print your scrollView.frame in -viewDidLoad() this will actually return the frame before your view will layout it's subviews. 如果您将在-viewDidLoad()中打印您的scrollView.frame,则实际上将返回该框架,然后视图将对其子视图进行布局。

So u need to make constraints to imageView OR in viewWillLayout redefine imageView.frame. 因此,您需要在viewWillLayout中对imageView进行约束或重新定义imageView.frame。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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