ios - ImageView 的边界在 ViewDidLayoutSubviews() 中为零

标签 ios swift uiscrollview uiimageview

<分区>

我正在关注 Raywenderlich 关于 UIScrollView 的教程,它可以平移和缩放图像 (https://www.raywenderlich.com/159481/uiscrollview-tutorial-getting-started)。除了我以编程方式创建 View 和布局约束外,我的代码与他们的代码完全相同。我的不工作。

我发现问题是我的 ImageViewviewDidLayoutSubviews() 中有一个零 bounds 值。但是,无论我是在 Storyboard 中还是在代码中创建约束,应该没有任何区别,对吧?如果是这样,将不胜感激任何解决方法的建议。 viewDidLoad()部分是我自己的代码。

class ComposeController: UIViewController {

    let scrollView = UIScrollView()
    let imageView = UIImageView()
    var imageViewBottomConstraint: NSLayoutConstraint!
    var imageViewLeadingConstraint: NSLayoutConstraint!
    var imageViewTopConstraint: NSLayoutConstraint!
    var imageViewTrailingConstraint: NSLayoutConstraint!

    override func viewDidLoad() {
        super.viewDidLoad()

        imageView.image = MyImage //from Assets
        scrollView.delegate = self       
        view.addSubview(scrollView)
        scrollView.addSubview(imageView)

        scrollView.translatesAutoresizingMaskIntoConstraints = false
        scrollView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
        scrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
        scrollView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
        scrollView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true

        imageView.translatesAutoresizingMaskIntoConstraints = false
        imageViewTopConstraint = imageView.topAnchor.constraint(equalTo: scrollView.topAnchor)
        imageViewBottomConstraint = imageView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor)
        imageViewLeadingConstraint = imageView.leftAnchor.constraint(equalTo: scrollView.leftAnchor)
        imageViewTrailingConstraint = imageView.rightAnchor.constraint(equalTo: scrollView.rightAnchor)
        imageViewTopConstraint.isActive = true
        imageViewBottomConstraint.isActive = true
        imageViewLeadingConstraint.isActive = true
        imageViewTrailingConstraint.isActive = true

    }

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        updateMinZoomScaleForSize(view.bounds.size)
    }

    fileprivate func updateMinZoomScaleForSize(_ size: CGSize) {
        let widthScale = size.width / imageView.bounds.width
        let heightScale = size.height / imageView.bounds.height
        let minScale = min(widthScale, heightScale)

        scrollView.minimumZoomScale = minScale
        scrollView.zoomScale = minScale
    }
}

extension ComposeController: UIScrollViewDelegate {
    func viewForZooming(in scrollView: UIScrollView) -> UIView? {
        return imageView
    }
}

最佳答案

根据 Apple 的 viewDidLayoutSubviews() 文档:

When the bounds change for a view controller's view, the view adjusts the positions of its subviews and then the system calls this method. However, this method being called does not indicate that the individual layouts of the view's subviews have been adjusted. Each subview is responsible for adjusting its own layout.

您可以通过调用获取 View 以布局各个 subview :

self.view.setNeedsLayout()
self.view.layoutIfNeeded()

关于ios - ImageView 的边界在 ViewDidLayoutSubviews() 中为零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48428833/

相关文章:

ios - AVCaptureDevice 低光增强 swift

ios - SwiftUI 模态环境

ios - UIScrollView 在触摸 Action 时自动滚动到顶部

ios - swift UITableView 滞后于图像

php - 转换 iPhone 时间戳

objective-c - iOS 在哪里本地保存可以重新下载的图像?

swift - addsubview View 不可点击

ios - 使用 ScrollView 缩放图像 Swift ios

ios - UIImageViews 在单元格中重复

objective-c - 如何缩放自定义引脚 MKMapKit