ios - ScrollView 宽高比中的 UIImageView 不起作用

标签 ios swift uiscrollview uiimageview aspect-ratio

我在 Storyboard中定义了一个带有 socket 的 UIScrollView。这个 ScrollView 通过代码(不在 Storyboard上)添加了一个 UIImageView 。当我运行该应用程序时,图像会超出纵横比。一旦我触摸缩放图像,它就会捕捉到正确的宽高比。

这是怎么回事?!

@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


}

加载时: enter image description here

完全移动缩放后: enter image description here

几乎固定: 我编辑了代码以在 viewWillLayoutSubviews 而不是 viewDidLoad 中定义 imageView.frame,如下面的答案中所建议的。我现在遇到了一个新问题 - 图像无法放大...它允许您缩放,直到松开手指,然后它会恢复到原始大小。

这是否与scrollView.maximumZoomScale不够大有关?

更正后的代码:

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


}

最佳答案

在-viewDidLoad()方法中设置框架还为时过早

如果你将在 -viewDidLoad() 中打印你的 scrollView.frame,这实际上会在你的 View 布局它的 subview 之前返回框架。

因此您需要对 imageView 进行约束,或者在 viewWillLayout 中重新定义 imageView.frame。

关于ios - ScrollView 宽高比中的 UIImageView 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32951205/

相关文章:

swift - 验证经度 double 值

ios - 持有对使用闭包的本地对象的强引用 [Swift]

iOS 7.1 为什么我的 UIScrollView 在显示键盘时滚动?

ios - NSDate 奇怪的小时

ios - Xcode 4 - 已添加、修改或删除签名资源

ios - 如何将数据存储到 Firestore 中具有相同 ID 的多个集合?

iOS 8 - 最初从 View 中隐藏 UITableView 的 headerView

ios - 在 iPhone 的单个 View Controller 中的一个 UIScrollView 中添加两个表

iphone - 如何在 iOS 应用中安全地包含服务器凭据

ios - 在 ScrollView 上的 View 上添加约束显示异常