【问题标题】:UIScrollView behaves differently when embedded in a navigation controllerUIScrollView 在嵌入导航控制器时表现不同
【发布时间】:2015-05-29 21:56:23
【问题描述】:

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

为什么会发生这种情况,我该如何解决?我已经看到其他建议设置内容大小的问题 [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 现在会出现在独立视图中以及嵌入导航控制器时。但是,现在嵌入式控制器的滚动视图顶部有一个额外的、不需要的空间。我认为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

【问题讨论】:

    标签: ios swift uiscrollview uinavigationcontroller autolayout


    【解决方案1】:

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

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

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

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

    6. 然后添加您想要添加的任何内容并从菜单中添加约束。

    7. 在 main.Storyboard 中单击您的视图控制器,然后在您的视图控制器中为您的滚动视图创建一个 @IBOutlet。

    8. 然后将这些行添加到您的 viewDidload() 作为 scrollView(在我的情况下)

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

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

    【讨论】:

      【解决方案2】:

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

      self.view.setTranslatesAutoresizingMaskIntoConstraints(false)
      

      ...并添加这个:

      self.automaticallyAdjustsScrollViewInsets = false
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-07-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-11-05
        • 2018-11-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多