具有StackView的ScrollView以编程方式无法滚动

时间:2018-11-24 00:44:18

标签: swift uiscrollview scrollview

我正在以编程方式制作一个UIScrollView,其中包含UIStackView

UIScrollView不会滚动,即使其内部的UIStacVview具有更大的宽度。

这是我的代码:

    view.backgroundColor = UIColor(red: 0.161, green: 0.165, blue: 0.188, alpha: 1.00) // 292a30

    view1.backgroundColor = UIColor.green
    view2.backgroundColor = UIColor.yellow
    view3.backgroundColor = UIColor.gray
    view4.backgroundColor = UIColor.white
    view5.backgroundColor = UIColor.orange

    scrollView.backgroundColor = UIColor.red
    scrollView.frame = CGRect(x: 10, y: 100, width: view.frame.width - 20, height: 100)
    scrollView.translatesAutoresizingMaskIntoConstraints = false

    stackView.backgroundColor = UIColor.blue
    stackView.axis = .horizontal
    stackView.spacing = 10
    stackView.distribution = .fillEqually
    stackView.frame = CGRect(x: 0, y: 0, width: view.frame.width + 200, height: 100)
    stackView.addArrangedSubview(view1)
    stackView.addArrangedSubview(view2)
    stackView.addArrangedSubview(view3)
    stackView.addArrangedSubview(view4)
    stackView.addArrangedSubview(view5)

    scrollView.addSubview(stackView)
    view.addSubview(scrollView)

工作原理:

  1. UIStackView加载并如下所示:

  1. UIStackView伸出UIScrollView的一侧。

什么不起作用:

  1. 您无法滚动UIScrollView,就像没有滚动一样。

2 个答案:

答案 0 :(得分:0)

1-使用框架布局时,请不要设置:

scrollView.translatesAutoresizingMaskIntoConstraints = false 

(这是当您通过编程方式设置约束时。)

2-对于scrollView,请使用:

scrollView.contentSize = CGSize(width: 200 * 5, height: 100)

(上面我假设每个视图的宽度为200,并且它们的数量等于5。)

OR

使用约束。

答案 1 :(得分:0)

经过各种尝试,我能够使其水平滚动并且没有重叠。

func createHorizontalStackViewsWithScroll() {

 self.view.addSubview(stackScrollView)
 stackScrollView.translatesAutoresizingMaskIntoConstraints = false
 stackScrollView.heightAnchor.constraint(equalToConstant: 85).isActive = true
 stackScrollView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor).isActive = true
 stackScrollView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor).isActive = true
 stackScrollView.bottomAnchor.constraint(equalTo: visualEffectViews.topAnchor).isActive = true
 stackScrollView.addSubview(stackView)

 stackView.translatesAutoresizingMaskIntoConstraints = false
 stackView.topAnchor.constraint(equalTo: stackScrollView.topAnchor).isActive = true
 stackView.leadingAnchor.constraint(equalTo: stackScrollView.leadingAnchor).isActive = true
 stackView.trailingAnchor.constraint(equalTo: stackScrollView.trailingAnchor).isActive = true
 stackView.bottomAnchor.constraint(equalTo: stackScrollView.bottomAnchor).isActive = true
 stackView.heightAnchor.constraint(equalTo: stackScrollView.heightAnchor).isActive = true

 stackView.distribution = .equalSpacing
 stackView.spacing = 5
 stackView.axis = .horizontal
 stackView.alignment = .fill


 for i in 0 ..< images.count {
 let photoView = UIButton.init(frame: CGRect(x: 0, y: 0, width: 85, height: 85))
 // set button image
 photoView.translatesAutoresizingMaskIntoConstraints = false
 photoView.heightAnchor.constraint(equalToConstant: photoView.frame.height).isActive = true
 photoView.widthAnchor.constraint(equalToConstant: photoView.frame.width).isActive = true

 stackView.addArrangedSubview(photoView)
 }
 stackView.setNeedsLayout()

 }