【问题标题】:Programmatically UIView with error Scrollable content size is ambiguous for UIScrollView. Only the first time以编程方式出现错误的 UIView 可滚动内容大小对于 UIScrollView 来说是不明确的。只有第一次
【发布时间】:2019-12-07 11:07:56
【问题描述】:

我的 UIViewController 遇到了一个奇怪的行为,它包含一个 UIScrollView。 ViewController 是从另一个 ViewController 推送而来的。视图的布局如下

  • UI 视图
    • UIScrollView
      • UI 视图
        • UIImageVIew
          • UILabel
        • UIStackView

我设置约束和更新stackView内容的方式如下。

private extension PresentationViewController {

    func setupView() {
        isScrolling = true

        view.addSubview(scrollView)
        scrollView.snp.makeConstraints { (builder) in
            builder.top.left.bottom.right.equalToSuperview()
        }

        scrollView.addSubview(contenView)
        contenView.snp.makeConstraints { (builder) in
            builder.top.left.bottom.right.equalToSuperview()
            builder.width.equalToSuperview()
        }

        contenView.addSubview(headerImageView)
        headerImageView.snp.makeConstraints { (builder) in
            builder.top.left.right.equalToSuperview()
            builder.height.equalTo(view.frame.height / 2)
        }

        headerImageView.addSubview(headerTitleLabel)
        headerTitleLabel.snp.makeConstraints { (builder) in
            builder.left.right.equalToSuperview()
            builder.centerX.equalToSuperview()
            builder.centerY.equalToSuperview()
        }

        contenView.addSubview(presentationStackView)
        presentationStackView.snp.makeConstraints { (builder) in
            builder.top.equalTo(headerImageView.snp.bottom).inset(-Margins.xxLarge)
            builder.left.right.equalToSuperview()
            builder.bottom.equalTo(scrollView.snp.bottom)
        }

    }

    func updateView(presentationResponse: PresentationResponse?) {
        guard let presentationResponse = presentationResponse else { return }

        let firstPresentation = presentationResponse.presentationItems[0]
        titleView.title = presentationResponse.galleryName

        headerImageView.sd_setImage(with: URL(string: firstPresentation.imageSet.fullSize ?? ""), placeholderImage: nil)
        headerTitleLabel.text = presentationResponse.galleryName

        for item in presentationResponse.presentationItems.dropFirst() {

            let sectionImageView = UIImageView()
            sectionImageView.contentMode = .scaleAspectFit
            sectionImageView.clipsToBounds = true

            let sectionCaptionLabel = BaseLabel(withConfiguration: .headline)
            sectionCaptionLabel.text = item.rowCaption

            let sectionView = UIView()
            sectionView.addSubview(sectionImageView)
            sectionView.addSubview(sectionCaptionLabel)

            sectionImageView.sd_setImage(with: URL(string: item.imageSet.defaultImage ?? "")) { [weak self, weak sectionView] (image, error, _, _) in

                guard let strongSelf = self,
                    let sectionView = sectionView,
                    let image = image else { return }

                strongSelf.presentationStackView.addArrangedSubview(sectionView)

                sectionImageView.snp.makeConstraints { (builder) in
                    builder.top.left.bottom.equalToSuperview().inset(Margins.medium)
                    let width:CGFloat = strongSelf.view.frame.size.width / 3
                    builder.width.equalTo(width)
                    builder.height.equalTo(width * (1 / image.aspectRatioValue))

                }

                sectionCaptionLabel.snp.makeConstraints { (builder) in
                    builder.left.equalTo(sectionImageView.snp.right).inset(-Margins.medium)
                    builder.top.right.equalToSuperview().inset(Margins.medium)
                }

            }

        }

    }

}

当对后端的异步操作完成时,在 ViewDidLoad 和 updateView(:) 中触发 setupView()。

您可能已经看到,我使用 Snapkit 来进行约束。 使用调试器工具检查我的 UI 时,我收到以下消息:

Scrollable content size is ambiguous for UIScrollView

这个问题似乎与 UIStackView 有关,因为它没有显示。 但是,如果我返回并再次推送,则不会出现错误,并且一切都正确显示。

【问题讨论】:

    标签: swift uiscrollview uikit uistackview snapkit


    【解决方案1】:

    如果布局显示正确,你真的不需要担心

    Scrollable content size is ambiguous for UIScrollView
    

    调试视图层次结构中的警告。但是,如果您想摆脱它,请在堆栈视图设置中添加一行:

        presentationStackView.snp.makeConstraints { (builder) in
            builder.top.equalTo(headerImageView.snp.bottom).inset(-Margins.xxLarge)
            builder.left.right.equalToSuperview()
            builder.bottom.equalTo(scrollView.snp.bottom)
    
            // add this line
            builder.height.equalTo(0).priority(250)
    
        }
    

    这将为堆栈视图提供一个有效的高度(为零),以便自动布局在它为空时考虑...但250 的低优先级将允许它在您添加排列的子视图时垂直扩展。

    【讨论】:

      【解决方案2】:

      只需将约束 Equal Height 和 Equal Width 添加到与主视图相关的 StackView 视图中,而不是添加到 UIScrollView 中。换句话说,Container View 的约束与 UIScrollView 的 superview 相关联。

      之后,您的 Storyboard 中将不会出现警告,您可以继续为子视图添加约束。

      【讨论】:

      • 谢谢。我没有使用故事板。所有的用户界面都是程序化的。你能写下你的意思吗?谢谢
      猜你喜欢
      • 2019-11-23
      • 2021-05-06
      • 2021-07-12
      • 1970-01-01
      • 1970-01-01
      • 2023-03-12
      • 1970-01-01
      • 1970-01-01
      • 2011-01-15
      相关资源
      最近更新 更多