Search code examples
swiftxcodeuistackview

(Swift) StackView in ScrollView doesn't work


I want to make a stackView in scrollView.
In stackView, I wanted to added some another stackViews, but it didn't work.

So, to simplify, I added some UIViews with addArrangedSubview() but it doesn't show anything.
How can I solve this? I spent a lot of my time...

        scrollView = UIScrollView()
        view.addSubview(scrollView)
        scrollView.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            scrollView.widthAnchor.constraint(equalTo: view.safeAreaLayoutGuide.widthAnchor),
            scrollView.heightAnchor.constraint(equalTo: view.safeAreaLayoutGuide.heightAnchor),
            scrollView.centerXAnchor.constraint(equalTo: view.safeAreaLayoutGuide.centerXAnchor),
            scrollView.centerYAnchor.constraint(equalTo: view.safeAreaLayoutGuide.centerYAnchor)
        ])
        
        
        var line1 = UIView(frame: CGRect(x: 0, y: 0, width: self.view.bounds.width, height: 1000))
        line1.backgroundColor = .blue
        var line2 = UIView(frame: CGRect(x: 0, y: 0, width: self.view.bounds.width, height: 1000))
        line2.backgroundColor = .green
        
        
        profile()
        
        stackView = UIStackView()
        stackView.axis = .vertical
        stackView.distribution = .fill
        stackView.alignment = .fill
        stackView.spacing = 10
        scrollView.addSubview(stackView)
        
        
        stackView.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            stackView.topAnchor.constraint(equalTo: scrollView.topAnchor),
            stackView.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor),
            stackView.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor),
            stackView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor)
        ])
        
        stackView.addArrangedSubview(line1)
        stackView.addArrangedSubview(line2)
        
        stackView.updateConstraints()
        stackView.setNeedsLayout()

Solution

  • I guess at the moment this code is running, the ViewControllers - view is still not set, so the width of your line1/2 will result in 0. You should use auto layout to layout your views too.

    var line1 = UIView()
    line1.translatesAutoresizingMaskIntoConstraints = false 
    line1.backgroundColor = .blue
    
    var line2 = UIView()
    line1.translatesAutoresizingMaskIntoConstraints = false 
    line2.backgroundColor = .green
        
    stackView = UIStackView(arrangedSubviews: [line1, line2])
    stackView.axis = .vertical
    stackView.distribution = .fill
    stackView.alignment = .fill
    stackView.spacing = 10
    scrollView.addSubview(stackView)
    
    stackView.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            stackView.topAnchor.constraint(equalTo: scrollView.topAnchor),
            stackView.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor),
            stackView.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor),
            stackView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor),
    
            line1.widthAnchor.constraint(equalTo: view.widthAnchor),
            line1.heightAnchor.constraint(equalToConstant: 1000),
    
            line2.widthAnchor.constraint(equalTo: view.widthAnchor),
            line2.heightAnchor.constraint(equalToConstant: 1000),
        ])