UIScrollView 嵌入导航控制器时表现不同

UIScrollView behaves differently when embedded in a navigation controller

在这个视图控制器中,我初始化了一个 UIScrollView,将它添加到层次结构中并在 viewDidLoad 中添加了自动布局约束,并在 viewDidLayoutSubviews 中添加了一个图像子视图。如果我将它分配给 Storyboard 中的独立视图,它会按预期运行(带有图像的方形滚动视图,我可以四处滚动)。但是,如果我将这个视图嵌入到导航控制器中,不做任何其他更改,我会得到一个黑屏,其中充满 window,没有图像。

为什么会发生这种情况,我该如何解决?我看过其他建议设置内容大小的问题 [1] [2]。但是,我正在尝试使用自动布局而不是直接设置内容大小。

这是一个最小但完整的示例。再次:使用独立视图,而不是嵌入时:

import UIKit

class ViewController: UIViewController, UIScrollViewDelegate {

    var scrollView: UIScrollView = UIScrollView(frame: CGRectZero)

    override func viewDidLoad() {
        super.viewDidLoad()

        self.scrollView.delegate = self
        self.scrollView.setTranslatesAutoresizingMaskIntoConstraints(false)
        self.view.addSubview(self.scrollView)
        self.scrollView.maximumZoomScale = 2
        self.scrollView.minimumZoomScale = 1

        self.view.setTranslatesAutoresizingMaskIntoConstraints(false)

        self.configureAutolayout()
    }

    override func viewDidLayoutSubviews() {
        if let x = UIImage(named: "photo.jpg") {
            let y = UIImageView(image: x)
            self.scrollView.addSubview(y)
        }
    }

    func configureAutolayout() {

        var constraint = NSLayoutConstraint(item: self.scrollView,
            attribute: .Leading,
            relatedBy: .Equal,
            toItem: self.view,
            attribute: .Leading,
            multiplier: 1,
            constant: 0)
        self.view.addConstraint(constraint)

        constraint = NSLayoutConstraint(item: self.scrollView,
            attribute: .Trailing,
            relatedBy: .Equal,
            toItem: self.view,
            attribute: .Trailing,
            multiplier: 1,
            constant: 0)
        self.view.addConstraint(constraint)

        constraint = NSLayoutConstraint(item: self.scrollView,
            attribute: .Top,
            relatedBy: .Equal,
            toItem: self.topLayoutGuide,
            attribute: .Bottom,
            multiplier: 1,
            constant: 0)
        self.view.addConstraint(constraint)

        constraint = NSLayoutConstraint(item: self.scrollView,
            attribute: .Height,
            relatedBy: .Equal,
            toItem: self.scrollView,
            attribute: .Width,
            multiplier: 1,
            constant: 0)
        self.view.addConstraint(constraint)

    }

    func viewForZoomingInScrollView(scrollView: UIScrollView!) -> UIView! {
        return self.scrollView.subviews.first as UIView
    }

}

编辑:

删除以下行修复了一个问题并引入了另一个问题:

self.view.setTranslatesAutoresizingMaskIntoConstraints(false)

带有图像子视图的 UIScrollView 现在既出现在独立视图中,也出现在嵌入导航控制器时。但是,现在在嵌入式控制器的滚动视图顶部有一个额外的、不需要的 space。我认为 TopLayoutGuide 是对齐的合适视图——不是这样吗?

编辑 2:

根据这个问题 [3],通过将 self.automaticallyAdjustsScrollViewInsets 设置为 false 解决了插入问题。它没有按预期运行。

参考文献:

[1] UIScrollView not scrolling when included into viewcontroller embedded into a navigation controller

[2]UIScrollView scroll not working after pushed with a navigation controller

[3]Explaining difference between automaticallyAdjustsScrollViewInsets, extendedLayoutIncludesOpaqueBars, edgesForExtendedLayout in iOS7

根据以上两次编辑,通过删除此行解决了问题...

self.view.setTranslatesAutoresizingMaskIntoConstraints(false)

...并添加这个:

self.automaticallyAdjustsScrollViewInsets = false

在 iOS10 它适合我。我正在构建一个项目,我想在其中添加导航控制器中的滚动视图

  1. 首先在导航控制器的视图中添加 ScrollView。
  2. 然后在 scrollview 上从 pin 添加约束为(0 从顶部,0 从左侧,0 从右侧,0 从底部)。
  3. 然后是Scrollview中的另一个Uiview(结构如下图)。

  4. 然后在 scrollView 下的视图上添加约束,如下所示(“等宽,等高,在容器中垂直居中,在容器中水平居中)..

  5. 另外还从底部和右侧的引脚添加一些约束(分别为 200 和 0)。如图...

  6. 然后添加任何你想添加的东西,并从菜单中添加约束。

  7. 在main.Storyboard中点击你的viewcontroller,然后在你的viewController中为你的滚动视图创建一个@IBOutlet。

  8. 然后将这些行添加到您的 viewDidload() 作为 scrollView(在我的例子中)

    let testing : CGFloat = 1000  
    scrollView.contentSize.height = testing
    

希望这对你有用..!!!