简体   繁体   English

为什么我的垂直编程滚动视图在 Swift 中不起作用

[英]Why doesn't my vertical programmatic scroll view work in Swift

I have created a test project to see if I can get a swift programmatic scroll view to work and I just cannot get the test data to scroll.我已经创建了一个测试项目,看看我是否可以让 swift 程序化滚动视图工作,但我就是无法让测试数据滚动。 Here is my test code.这是我的测试代码。

import UIKit

class ViewController: UIViewController {

    private let viewLabel0: UILabel = {
        let label = UILabel()
        label.text = "Stephen 0"
        label.font = UIFont.systemFont(ofSize: 30)
        label.textColor = .white
        return label
    }()
    

    private let viewLabel1: UILabel = {
        let label = UILabel()
        label.text = "Stephen 1"
        label.font = UIFont.systemFont(ofSize: 30)
        label.textColor = .white
        return label
    }()

    private let viewLabel2: UILabel = {
        let label = UILabel()
        label.text = "Stephen 2"
        label.font = UIFont.systemFont(ofSize: 30)
        label.textColor = .white
        return label
    }()

    private let viewLabel3: UILabel = {
        let label = UILabel()
        label.text = "Stephen 3"
        label.font = UIFont.systemFont(ofSize: 30)
        label.textColor = .white
        return label
    }()

    private let viewLabel4: UILabel = {
        let label = UILabel()
        label.text = "Stephen 4"
        label.font = UIFont.systemFont(ofSize: 30)
        label.textColor = .white
        return label
    }()
    
    private let viewLabel5: UILabel = {
        let label = UILabel()
        label.text = "Stephen 5"
        label.font = UIFont.systemFont(ofSize: 30)
        label.textColor = .white
        return label
    }()

    private let viewLabel6: UILabel = {
        let label = UILabel()
        label.text = "Stephen 6"
        label.font = UIFont.systemFont(ofSize: 30)
        label.textColor = .white
        return label
    }()
    

    private let viewLabel7: UILabel = {
        let label = UILabel()
        label.text = "Stephen 7"
        label.font = UIFont.systemFont(ofSize: 30)
        label.textColor = .white
        return label
    }()

    private let viewLabel8: UILabel = {
        let label = UILabel()
        label.text = "Stephen 8"
        label.font = UIFont.systemFont(ofSize: 30)
        label.textColor = .white
        return label
    }()

    private let viewLabel9: UILabel = {
        let label = UILabel()
        label.text = "Stephen 9"
        label.font = UIFont.systemFont(ofSize: 30)
        label.textColor = .white
        return label
    }()

    private let viewLabel10: UILabel = {
        let label = UILabel()
        label.text = "Stephen 10"
        label.font = UIFont.systemFont(ofSize: 30)
        label.textColor = .white
        return label
    }()
    
    private let viewLabel11: UILabel = {
        let label = UILabel()
        label.text = "Stephen 11"
        label.font = UIFont.systemFont(ofSize: 30)
        label.textColor = .white
        return label
    }()

    private let viewLabel12: UILabel = {
        let label = UILabel()
        label.text = "Stephen 12"
        label.font = UIFont.systemFont(ofSize: 30)
        label.textColor = .white
        return label
    }()

    private let viewLabel13: UILabel = {
        let label = UILabel()
        label.text = "Stephen 13"
        label.font = UIFont.systemFont(ofSize: 30)
        label.textColor = .white
        return label
    }()
    
    private let viewLabel14: UILabel = {
        let label = UILabel()
        label.text = "Stephen 14"
        label.font = UIFont.systemFont(ofSize: 30)
        label.textColor = .white
        return label
    }()
    
    private lazy var scrollView: UIScrollView = {
        let sv = UIScrollView()
        sv.isScrollEnabled = true
        sv.contentSize = CGSize(width: self.view.frame.width, height: self.view.frame.height * 3.0)
        return sv
    }()
    
    private let contentView: UIView = {
        let cv = UIView()
        return cv
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = UIColor.mainBackgroundColor
        
        view.addSubview(scrollView)
        scrollView.anchor(top: view.topAnchor,
                          left: view.leftAnchor,
                          bottom: view.bottomAnchor,
                          right: view.rightAnchor)
        
        scrollView.addSubview(contentView)
        contentView.anchor(top: scrollView.contentLayoutGuide.topAnchor,
                           left: scrollView.frameLayoutGuide.leftAnchor,
                           bottom: scrollView.contentLayoutGuide.bottomAnchor,
                           right: scrollView.frameLayoutGuide.rightAnchor,
                           paddingTop: 0,
                           paddingLeft: 0,
                           paddingBottom: 0,
                           paddingRight: 0)

        
        contentView.addSubview(viewLabel0)
        viewLabel0.anchor(top: view.safeAreaLayoutGuide.topAnchor, paddingTop: 40.0)
        viewLabel0.centerX(inView: contentView)

        contentView.addSubview(viewLabel1)
        viewLabel1.anchor(top: viewLabel0.bottomAnchor, paddingTop: 40.0)
        viewLabel1.centerX(inView: contentView)

        contentView.addSubview(viewLabel2)
        viewLabel2.anchor(top: viewLabel1.bottomAnchor, paddingTop: 40.0)
        viewLabel2.centerX(inView: contentView)

        contentView.addSubview(viewLabel3)
        viewLabel3.anchor(top: viewLabel2.bottomAnchor, paddingTop: 40.0)
        viewLabel3.centerX(inView: contentView)

        contentView.addSubview(viewLabel4)
        viewLabel4.anchor(top: viewLabel3.bottomAnchor, paddingTop: 40.0)
        viewLabel4.centerX(inView: contentView)

        contentView.addSubview(viewLabel5)
        viewLabel5.anchor(top: viewLabel4.bottomAnchor, paddingTop: 40.0)
        viewLabel5.centerX(inView: contentView)

        contentView.addSubview(viewLabel6)
        viewLabel6.anchor(top: viewLabel5.bottomAnchor, paddingTop: 40.0)
        viewLabel6.centerX(inView: contentView)

        contentView.addSubview(viewLabel7)
        viewLabel7.anchor(top: viewLabel6.bottomAnchor, paddingTop: 40.0)
        viewLabel7.centerX(inView: contentView)

        contentView.addSubview(viewLabel8)
        viewLabel8.anchor(top: viewLabel7.bottomAnchor, paddingTop: 40.0)
        viewLabel8.centerX(inView: contentView)

        contentView.addSubview(viewLabel9)
        viewLabel9.anchor(top: viewLabel8.bottomAnchor, paddingTop: 40.0)
        viewLabel9.centerX(inView: contentView)

        contentView.addSubview(viewLabel10)
        viewLabel10.anchor(top: viewLabel9.bottomAnchor, paddingTop: 40.0)
        viewLabel10.centerX(inView: contentView)

        contentView.addSubview(viewLabel11)
        viewLabel11.anchor(top: viewLabel10.bottomAnchor, paddingTop: 40.0)
        viewLabel11.centerX(inView: contentView)

        contentView.addSubview(viewLabel12)
        viewLabel12.anchor(top: viewLabel11.bottomAnchor, paddingTop: 40.0)
        viewLabel12.centerX(inView: contentView)

        contentView.addSubview(viewLabel13)
        viewLabel13.anchor(top: viewLabel12.bottomAnchor, paddingTop: 40.0)
        viewLabel13.centerX(inView: contentView)

        contentView.addSubview(viewLabel14)
        viewLabel14.anchor(top: viewLabel13.bottomAnchor, paddingTop: 40.0)
        viewLabel14.centerX(inView: contentView)

        
        
        
    }


}

I feel I have tried everything from looking in stack overflow to searching online but I simply cannot get it to scroll the list of labels I have created.我觉得我已经尝试了从查看堆栈溢出到在线搜索的所有方法,但我就是无法让它滚动我创建的标签列表。

First of all you must not set your view properties in viewDidLoad, because in this method the view is loaded not appeared, try to put same code in viewDidAppear and also check content size property of you scroll view, it should match with your scroll content size and should be greater than you view size to scroll and also keep scrolling enable true for scroll view.首先你不能在 viewDidLoad 中设置你的视图属性,因为在这个方法中视图加载没有出现,尝试将相同的代码放在 viewDidAppear 中并检查你滚动视图的内容大小属性,它应该与你的滚动内容大小匹配并且应该大于您查看滚动的大小,并且还保持滚动为滚动视图启用 true。

Second thing it would be better to take scroll from you storyboard.第二件事最好从你 storyboard 那里拿走卷轴。

Refer the link for implementation, I search on google you can also try other links.参考实现的链接,我在谷歌上搜索你也可以尝试其他链接。

https://medium.com/@pradeep_chauhan/how-to-configure-a-uiscrollview-with-auto-layout-in-interface-builder-218dcb4022d7 https://medium.com/@pradeep_chauhan/how-to-configure-a-uiscrollview-with-auto-layout-in-interface-builder-218dcb4022d7

Needed the following code需要以下代码

       view.addSubview(scrollView)
        scrollView.anchor(top: self.view.topAnchor,
                          bottom: self.view.bottomAnchor,
                          paddingTop: 0,
                          paddingBottom: 0)
        scrollView.trail(left: self.view.leadingAnchor,
                         right: self.view.trailingAnchor,
                         leftT: 0,
                         rightT: 0)
        
        scrollView.addSubview(contentView)
        contentView.topAnchor.constraint(equalTo: scrollView.topAnchor).isActive = true;
        contentView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor).isActive = true;
        contentView.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor).isActive = true;
        contentView.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor).isActive = true;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM