【问题标题】:Setting Dynamic Height to UIScrollView Swift将动态高度设置为 UIScrollView Swift
【发布时间】:2017-04-18 02:51:17
【问题描述】:

如何根据我的 ScrollView 中的内容/子视图设置 UIScrollView 的动态高度

这是我得到的输出:

【问题讨论】:

    标签: ios uiscrollview swift2 height


    【解决方案1】:

    我们可以使用自动布局,我们在滚动视图中使用内容视图,可以将其固定到滚动视图,并且给定的高度和宽度约束等于主视图。然后,如果我们希望高度动态变化,我们可以给高度约束一个比其他约束低的优先级,因此内容视图的高度将根据其固有大小增加,因为它的子视图。

    参考链接:

    1) https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/AutolayoutPG/WorkingwithScrollViews.html#//apple_ref/doc/uid/TP40010853-CH24-SW1

    2) https://www.youtube.com/watch?v=6J22gHORk2I

    【讨论】:

      【解决方案2】:

      首先你需要设置0的子视图高度约束,然后计算数据高度(你想在子视图中设置),并简单地分配subview.heightConstraint.constant = data.height,然后你需要设置scrollView.contentsize,它基于子视图身高:

      self.scrollView.contentSize = CGSize(width: self.scrollView.contentSize.width, height: self.subViewHeight.constant)
      

      【讨论】:

      • 你有什么教程要学吗@vaibhav
      【解决方案3】:

      对我来说,这个技巧奏效了(Swift 4)

      scrollView.layoutIfNeeded()
      scrollView.isScrollEnabled = true
      scrollView.contentSize = CGSize(width: self.view.frame.width, height: scrollView.frame.size.height)
      

      【讨论】:

      • 你怎么称呼它/self.view代表什么?
      • @dv02 - 调用它的最佳位置是viewDidLayoutSubviews 方法。此外,高度应设置为contentView.frame.size.height 而不是scrollView.frame.size.height,因为滚动视图框架是主框架的。
      【解决方案4】:

      在我的情况下,我必须使用 textViews(和其他视图)构建一个滚动视图,它必须动态调整大小。
      在 InterfaceBuilder 中,我为 min 设置了约束。高度和固定高度。然后在构建时删除固定高度。
      这按预期工作。

      【讨论】:

        【解决方案5】:

        您必须计算内容所需的高度,包括间距。 将该值分配为行的高度并刷新表(或与行一起部分)

        【讨论】:

          【解决方案6】:

          垂直滚动视图根据标签文本长度调整大小的示例:

              let scrollView = UIScrollView()
              
              view.addSubview(scrollView)
              scrollView.translatesAutoresizingMaskIntoConstraints = false
          
              scrollView.heightAnchor.constraint(lessThanOrEqualToConstant: UIScreen.main.bounds.height / 2).isActive = true
          
              let bodyLabel = UILabel()
          
              scrollView.addSubview(bodyLabel)
              bodyLabel.translatesAutoresizingMaskIntoConstraints = false
          
              bodyLabel.topAnchor.constraint(equalTo: scrollView.topAnchor).isActive = true
              bodyLabel.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor).isActive = true
              bodyLabel.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor).isActive = true
              bodyLabel.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor).isActive = true
              bodyLabel.widthAnchor.constraint(equalTo: scrollView.widthAnchor).isActive = true
          
              let heightConstraint = bodyLabel.heightAnchor.constraint(equalTo: scrollView.heightAnchor)
              heightConstraint.priority = .defaultLow
              heightConstraint.isActive = true
          

          【讨论】:

            【解决方案7】:

            设置滚动视图的子视图到主视图的等宽和等高约束。 在 IB Size Inspector 中,将 Equal Heights Constraints - Priority 设置为 Low(250)

            【讨论】:

              猜你喜欢
              • 2018-08-18
              • 2017-12-17
              • 1970-01-01
              • 2017-12-30
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多