【问题标题】:Adding Stackview to UIScrollView将 Stackview 添加到 UIScrollView
【发布时间】:2017-12-19 17:55:25
【问题描述】:

我有一个UIScrollView。它有一个堆栈视图。这个堆栈视图包含 12 个按钮。 (水平滚动视图)

Stackview 约束:- 滚动视图的顶部、前导、尾随、底部以及与滚动视图相等的宽度。

我的问题是每次运行时,堆栈视图宽度限制为滚动视图宽度,并且按钮根据堆栈视图的宽度太小,并且我的滚动视图不可滚动。

如何使这个可滚动

【问题讨论】:

  • 您在滚动视图中有内容视图,还是直接将堆栈视图添加到滚动视图?见Apple's Guide to working with scrollviews
  • 不,我没有内容视图,我将堆栈视图直接添加到滚动视图@TheBaj
  • 好吧,不要那样做。阅读指南并按照步骤操作,您应该会没事的。
  • @TheBaj - UIStackViews 在直接添加到滚动视图时效果很好 - 只需正确设置约束。
  • 对,但如果他要向滚动视图添加更多内容而不仅仅是堆栈视图,那么使用内容视图会更好

标签: ios swift uiscrollview autolayout uistackview


【解决方案1】:

如果你让 Stackview 的宽度等于滚动视图的宽度,那么这就是你所得到的,当然它不会滚动。

不要给你的 Stackview 一个宽度限制...让按钮“填满”。


编辑:这是一个可以直接在 Playground 页面中运行的简单示例:

import UIKit
import PlaygroundSupport

class TestViewController : UIViewController {

    let scrollView: UIScrollView = {
        let v = UIScrollView()
        v.translatesAutoresizingMaskIntoConstraints = false
        v.backgroundColor = .cyan
        return v
    }()

    let stackView : UIStackView = {
        let v = UIStackView()
        v.translatesAutoresizingMaskIntoConstraints = false
        v.axis = .horizontal
        v.distribution = .equalSpacing
        v.spacing = 10.0
        return v
    }()


    override func viewDidLoad() {
        super.viewDidLoad()

        // add the scroll view to self.view
        self.view.addSubview(scrollView)

        // constrain the scroll view to 8-pts on each side
        scrollView.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 8.0).isActive = true
        scrollView.topAnchor.constraint(equalTo: view.topAnchor, constant: 8.0).isActive = true
        scrollView.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -8.0).isActive = true
        scrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -8.0).isActive = true


        // add the stack view to the scroll view 
        scrollView.addSubview(stackView)

        // constrain the stackview view to 8-pts on each side
        //   this *also* controls the .contentSize of the scrollview
        stackView.leftAnchor.constraint(equalTo: scrollView.leftAnchor, constant: 8.0).isActive = true
        stackView.topAnchor.constraint(equalTo: scrollView.topAnchor, constant: 8.0).isActive = true
        stackView.rightAnchor.constraint(equalTo: scrollView.rightAnchor, constant: -8.0).isActive = true
        stackView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor, constant: -8.0).isActive = true

        // add ten buttons to the stack view
        for i in 1...10 {

            let b = UIButton()
            b.translatesAutoresizingMaskIntoConstraints = false
            b.setTitle("Button \(i)", for: .normal)
            b.backgroundColor = .blue
            stackView.addArrangedSubview(b)

        }

    }

}

let vc = TestViewController()
vc.view.backgroundColor = .yellow
PlaygroundPage.current.liveView = vc

【讨论】:

  • 所以我想给每个按钮一个宽度限制?
  • 您不需要,因为UIButton 具有基于标签的固有宽度。或者,您可以给它们明确的宽度限制。
  • 我删除了堆栈视图的宽度约束,也删除了滚动视图的等宽。并设置每个按钮的宽度约束。我通过代码width=120*12和heigth=scrollview.height来设置scrollview的内容大小。但滚动视图不滚动。
  • 不要设置.contentSize - 这就是约束的用途。确保您的堆栈视图分布设置为等间距。
  • distribution is Fill equal 我需要设置等间距吗?但我不希望这些按钮之间有空格
【解决方案2】:

在 IB/Storyboards 中进行设置的分步...

  1. 添加视图 - 高度为 50 的前导/顶部/尾部 - 蓝色背景

  1. 向该视图添加一个滚动视图 - 将前导/顶部/尾随/底部固定为 0 - 将滚动视图背景设置为黄色,以便我们可以看到它在哪里

  1. 向滚动视图添加一个按钮

  1. 复制它,这样你就有 12 个按钮

  1. 将它们分组到一个堆栈视图中,并将堆栈视图的约束设置为 0 前导/顶部/尾随/底部

  1. 并将堆栈视图的分布设置为“等间距”

  1. 在模拟器中运行的结果(完全没有代码):

按钮左右滚动....contentSize没有代码设置...

【讨论】:

  • 当我添加按钮并将它们分组到堆栈视图中然后设置约束时,然后我的按钮出现在滚动条下方,不确定是什么问题
【解决方案3】:

所以你想要这个:

这是我在 Xcode 8.3.3 中的做法。

  1. 新建项目 > iOS > 单视图应用程序。

  2. 打开 Main.storyboard。

  3. 将滚动视图拖到场景中。

  4. 将滚动视图的顶部、前导和尾部固定为 0。将高度设置为 30。

  5. 将水平堆栈视图拖到滚动视图中。

  6. 将堆栈视图的所有四个边缘固定为 0。

  7. 将堆栈视图间距设置为 4。

  8. 将十二个按钮拖到堆栈视图中。

  9. 将目标设备设置为 iPhone SE。

  10. 构建并运行。

生成的文档大纲:

【讨论】:

  • 谢谢,虽然我接受了第一个,但这也有效:) 但是我投票了
  • 我跟着这个,然后在模拟器中运行时,整个东西都没有显示出来。
猜你喜欢
  • 2012-02-14
  • 1970-01-01
  • 1970-01-01
  • 2015-06-29
  • 2023-03-28
  • 2010-12-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多