首页 > 解决方案 > (Swift) ScrollView 中的 StackView 不起作用

问题描述

我想在scrollView 中创建一个stackView。
在stackView中,我想再添加一些stackViews,但是没有用。

因此,为了简化,我添加了一些 UIViews,addArrangedSubview()但它没有显示任何内容。
我该如何解决这个问题?我花了很多时间...

        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()

标签: swiftxcodeuistackview

解决方案


我猜在这段代码正在运行的那一刻,ViewControllers - 视图仍未设置,所以你的 line1/2 的宽度将导致 0。你也应该使用自动布局来布局你的视图。

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),
    ])

推荐阅读