菜鸟教程小白 发表于 2022-12-11 18:52:53

ios - 以编程方式在 Swift 中显示 UIScrollView


                                            <p><p>我有带有 View 的 ViewController :顶层 View 包含容器 View ,其中包含两个 View :bottomView、topView,如图所示。</p>

<p> <a href="/image/GyD8K.png" rel="noreferrer noopener nofollow"><img src="/image/GyD8K.png" alt="view controller"/></a> </p>

<p>场景</p>

<p> <a href="/image/FCe3C.png" rel="noreferrer noopener nofollow"><img src="/image/FCe3C.png" alt="scene"/></a> </p>

<p>我想在 topView 中显示从:到:日期范围。在底部 View 中,我想显示一个 UIScrollView,其中包含我可以滚动的两列。我这样做了,但是当我引入 scrollView 时,topView 和 BottomView 重叠。当我滚动时,我可以看到 View 被分开,一旦我放开滚动条,它们就会再次重叠。</p>

<p> <a href="/image/KBnzu.png" rel="noreferrer noopener nofollow"><img src="/image/KBnzu.png" alt="enter image description here"/></a> </p>

<p>谁能告诉我如何解决它?我只是似乎不明白 scrollView 和 bottomView 是如何关联的。
代码如下:</p>

<pre><code>override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    scrollView.frame = view.bounds

    //scrollView.frame = innerView.bounds
    innerView.frame =   CGRect(x:0, y:0, width:scrollView.contentSize.width, height:scrollView.contentSize.height)
}
func buildBottomView () {
    let screenSize = UIScreen.main.bounds
    let screenWidth = screenSize.width
    let ht:Int = 21
    let incrX:Int = 5
    let incrY:Int = 5
    let gapCol1:Int = 5
    let col1Width:Int = 65
    let col2Width:Int = 65
    let startY:Int = 5
    let col1StartX:Int = 10
    let col2StartX:Int = col1StartX + col1Width + gapCol1

    var loadRowStartY: Int = 0
    // column headers
    categoryColumnLabel.text = &#34;Interval&#34;
    categoryColumnLabel.font = UIFont.preferredFont(forTextStyle:UIFontTextStyle.subheadline)
    //categoryColumnLabel.font = UIFont.systemFont(ofSize: 14)
    categoryColumnLabel.frame = CGRect(x: col1StartX, y:startY, width: col1Width, height: ht)
    categoryColumnLabel.textAlignment = NSTextAlignment.left
    categoryColumnLabel.tag = 1
    innerView.addSubview(categoryColumnLabel)

    valueColumnLabel.text = &#34;Values&#34;
    valueColumnLabel.font = UIFont.preferredFont(forTextStyle:UIFontTextStyle.subheadline)
    //valueColumnLabel.font = UIFont.systemFont(ofSize: 14)
    valueColumnLabel.frame = CGRect(x: col2StartX, y:startY, width: col2Width, height: ht)
    valueColumnLabel.textAlignment = NSTextAlignment.center
    valueColumnLabel.tag = 3
    innerView.addSubview(valueColumnLabel)

    let sepLine:UIView = UIView()
    sepLine.frame = CGRect(x: col1StartX, y:startY+ht+incrY, width: Int(screenWidth-20), height: 2)
    sepLine.backgroundColor = UIColor.darkGray
    sepLine.tag = 60

    loadRowStartY = startY+ht+incrX+ht
    innerView.addSubview(sepLine)



    for i in 0 ..&lt; 24 {

      let timeIntervalLabel = UILabel()
      let value2Label = UILabel()

      print(&#34;display load profile&#34;)

      let loadStruct= loadDict as! CommercialProfile
      print (loadStruct.timeInterval)

      timeIntervalLabel.text = loadStruct.timeInterval
      timeIntervalLabel.font = UIFont.preferredFont(forTextStyle:UIFontTextStyle.caption1)

      //valueColumnLabel.font = UIFont.systemFont(ofSize: 14)
      timeIntervalLabel.frame = CGRect(x: col1StartX, y:loadRowStartY, width: col1Width, height: Int(timeIntervalLabel.font.lineHeight))
      timeIntervalLabel.textAlignment = NSTextAlignment.center
      innerView.addSubview(timeIntervalLabel)

      print(loadStruct.value)

      value2Label.text = loadStruct.value
      value2Label.font = UIFont.preferredFont(forTextStyle:UIFontTextStyle.caption1)
      //value2Label = UIFont.systemFont(ofSize: 14)
      value2Label.frame = CGRect(x: col2StartX, y:loadRowStartY, width: col2Width, height: Int(value2Label.font.lineHeight))
      value2Label.textAlignment = NSTextAlignment.center
      innerView.addSubview(value2Label)

      loadRowStartY = loadRowStartY + incrY + Int(value2Label.font.lineHeight)

    }
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>您正在使用以下代码将 ScrollView 的边界设置为整个 View 的大小:<code>scrollView.frame = view.bounds</code>。</p>

<p>scrollView 只需要滚动底部 View 中的内容。 ScrollView 有自己的内容,通常大于屏幕/ View 的可视区域。 ScrollView 只允许您平移该 View 的视口(viewport)。</p>

<p>所以添加底部 View 并设置你的约束。将 ScrollView 添加到底部 View ,然后将您的内容添加到 ScrollView 中。 </p>

<p>确保您的底部 View 已将 <code>clipToBounds</code> 设置为 true,然后您应该能够将标题保持在原位并滚动内容。 </p>

<p>我会尽快为您整理一个示例。 </p>

<p><strong>编辑:</strong> </p>

<p>我刚刚创建了这个简单的示例,它显示了您需要的滚动行为。这适用于 Playground 或只是作为一个简单的 ViewController 。由于时间原因,我故意不使用自动布局或设置限制,但您会看到解决问题所需的内容</p>

<pre><code>class ViewController: UIViewController {

    var topView: UIView!
    var bottomView: UIView!
    var scrollView: UIScrollView!
    var contentView: UIView!

    override func viewDidLoad() {
      super.viewDidLoad()

      let screenSize = UIScreen.main.bounds

      self.topView = UIView(frame: CGRect(x: 0, y: 0, width: screenSize.width, height: 100))
      self.bottomView = UIView(frame: CGRect(x: 0, y: 100, width: screenSize.width, height: screenSize.height - 100))
      self.scrollView = UIScrollView(frame: CGRect(x: 0, y: 100, width: screenSize.width, height: screenSize.height - 100))
      self.contentView = UIView(frame: CGRect(x: 0, y: 0, width: screenSize.width, height: screenSize.height * 3))

      self.view.backgroundColor = .white
      self.view.addSubview(self.topView)
      self.view.addSubview(self.bottomView)
      self.bottomView.addSubview(self.scrollView)
      self.scrollView.addSubview(self.contentView)
      self.bottomView.clipsToBounds = true


      self.scrollView.contentSize = CGSize(width: screenSize.width, height: screenSize.height * 3)
      self.contentView.backgroundColor = .gray
    }
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 以编程方式在 Swift 中显示 UIScrollView,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/46525868/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/46525868/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 以编程方式在 Swift 中显示 UIScrollView