如何以编程方式在滚动视图中嵌入堆栈视图

时间:2016-01-22 11:59:20

标签: ios swift uiscrollview swift2 uistackview

我尝试过嵌入它,但我的堆栈视图是动态的,我的应用程序也在不断改变方向。我在视图的末尾有段控制。 我也试过谷歌搜索但没有运气。提前谢谢。

到目前为止,我已经完成了:

在视图中加载:

mainStackView.axis = UILayoutConstraintAxis.Vertical
mainStackView.spacing = 3
scrollView.frame =  self.view.bounds
scrollView.addSubview(mainStackView)
view.addSubview(scrollView)

在视图中做了布局:

override func viewDidLayoutSubviews()
    {
        super.viewDidLayoutSubviews()
        let top = topLayoutGuide.length
        let bottom = bottomLayoutGuide.length
                        self.mainStackView.frame = CGRect(x: 0, y: top, width: view.frame.width, height: view.frame.height - top - bottom).insetBy(dx: 10, dy: 10)
        dispatch_async(dispatch_get_main_queue())
            {

                self.scrollView.frame =  self.view.bounds
                self.scrollView.contentSize = CGSize(width: self.view.bounds.width, height: self.segmentedControl.frame.origin.y + self.segmentedControl.frame.height + 50)
        }
        print(scrollView.contentSize)
    }

1 个答案:

答案 0 :(得分:2)

您需要设置分段控制的高度约束。

例如:

 segmentedControl.heightAnchor.constraintEqualToConstant(50).active = true

此外,您可以添加空底视图以避免堆栈视图必须填充机制。这将显示所需的视图输出。

        var bottomView = UIView(frame: CGRectZero)
        stackView.addArrangedSubview(bottomView)