Swift:UIScrollView 不适用于 UILabel

时间:2021-08-01 21:27:54

标签: swift uiscrollview uikit

我想在 UILabel 中放置一个带有很长文本的 UIScrollView 以滚动其内容。为此,我有以下代码:

import UIKit

class InfoViewController: UIViewController {
    private let info : String =
        """
           A VERY LONG TEXT HERE
        """
    
    private let scrollView : UIScrollView = {
        let view = UIScrollView()
        view.translatesAutoresizingMaskIntoConstraints = false
        view.keyboardDismissMode = .onDrag
        view.isScrollEnabled = true
        return view
    }()

    private let containerView : UIView = {
        let view = UIView()
        view.translatesAutoresizingMaskIntoConstraints = false
        return view
    }()

    private let infoTitleLabel : UILabel = {
        let view = UILabel()
        view.translatesAutoresizingMaskIntoConstraints = false
        view.text = "Title"
        view.font = UIFont.systemFont(ofSize: 18, weight: .bold)
        return view
    }()

    private lazy var infoLabel : UILabel = {
        let view = UILabel()
        view.translatesAutoresizingMaskIntoConstraints = false
        view.numberOfLines = 0
        view.text = self.info
        return view
    }()
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        safeArea = view.layoutMarginsGuide
        
        setupView()
    }
    
    private var safeArea : UILayoutGuide!

    private func setupView()
    {
        view.backgroundColor = .white
        title = "Info"
        
        view.addSubview(scrollView)
        
        containerView.addSubview(infoTitleLabel)
        containerView.addSubview(infoLabel)
        
        scrollView.addSubview(containerView)
        
        let constraints = [
            scrollView.topAnchor.constraint(equalTo: safeArea.topAnchor),
            scrollView.bottomAnchor.constraint(equalTo: safeArea.bottomAnchor),
            scrollView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            scrollView.trailingAnchor.constraint(equalTo: view.trailingAnchor),

            containerView.topAnchor.constraint(equalTo: scrollView.topAnchor),
            containerView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor),
            containerView.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor),
            containerView.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor),
            containerView.widthAnchor.constraint(equalTo: scrollView.widthAnchor),
            containerView.heightAnchor.constraint(equalTo: scrollView.heightAnchor),

            infoTitleLabel.topAnchor.constraint(equalTo: safeArea.topAnchor, constant: 10),
            infoTitleLabel.leadingAnchor.constraint(equalTo: containerView.leadingAnchor, constant: 10),
            
            infoTitleLabel.heightAnchor.constraint(equalToConstant: 20),
            
            infoLabel.leadingAnchor.constraint(equalTo: infoTitleLabel.leadingAnchor),
            infoLabel.trailingAnchor.constraint(equalTo: containerView.trailingAnchor, constant: -10),
            infoLabel.topAnchor.constraint(equalTo: infoTitleLabel.bottomAnchor, constant: 10),
            infoLabel.bottomAnchor.constraint(equalTo: containerView.bottomAnchor)
        ]
        
        NSLayoutConstraint.activate(constraints)
    }
}

但我没有看到滚动发生。这是什么原因?是否与动态标签内容大小有关?

1 个答案:

答案 0 :(得分:1)

该问题与不正确的约束有关。这是正确的代码:

let constraints = [
    scrollView.topAnchor.constraint(equalTo: safeArea.topAnchor),
    scrollView.bottomAnchor.constraint(equalTo: safeArea.bottomAnchor),
    scrollView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
    scrollView.trailingAnchor.constraint(equalTo: view.trailingAnchor),

    containerView.topAnchor.constraint(equalTo: scrollView.topAnchor),
    containerView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor),
    containerView.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor),
    containerView.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor),
    containerView.widthAnchor.constraint(equalTo: scrollView.widthAnchor),

    infoTitleLabel.topAnchor.constraint(equalTo: containerView.topAnchor, constant: 10),
    infoTitleLabel.leadingAnchor.constraint(equalTo: containerView.leadingAnchor, constant: 10),
    
    infoTitleLabel.heightAnchor.constraint(equalToConstant: 20),
    
    infoLabel.leadingAnchor.constraint(equalTo: infoTitleLabel.leadingAnchor),
    infoLabel.trailingAnchor.constraint(equalTo: containerView.trailingAnchor, constant: -10),
    infoLabel.topAnchor.constraint(equalTo: infoTitleLabel.bottomAnchor, constant: 10),
    infoLabel.bottomAnchor.constraint(equalTo: containerView.bottomAnchor)
]