You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

UIScrollView无法垂直滚动及UITableView不显示问题求助

UIScrollView滚动及UITableView显示问题

我花了一整天尝试让UIScrollView实现垂直滚动,但始终没成功,也找不到问题所在。我查了相关解决方案,但视图还是无法滚动,scrollView高度固定为父视图高度,没有匹配contentView的高度。我已经把代码精简到只剩长文本标签,对比参考项目也没找出差异。

初始RecipeUIView代码

import Foundation
import UIKit

class RecipeUIView: UIView{

    private var recipeTitle: UILabel! = {
        
        let label = UILabel()
        label.numberOfLines = 0
        label.font = .systemFont(ofSize: 24, weight: .bold)
        label.textColor = .gray
        label.textAlignment = .center
        label.translatesAutoresizingMaskIntoConstraints = false
        label.adjustsFontForContentSizeCategory = true
        
        return label
    }()
    
    
    func setupView(currentRecipe: Receipe?){
        
        recipeTitle.text = currentRecipe?.dynamicTitle

        addSubview(recipeTitle)

        let margin = readableContentGuide
        
        // Constraints
        
        recipeTitle.topAnchor.constraint(equalTo: margin.topAnchor, constant: 4).isActive = true
        recipeTitle.leadingAnchor.constraint(equalTo: margin.leadingAnchor, constant: 20).isActive = true
        recipeTitle.trailingAnchor.constraint(equalTo: margin.trailingAnchor, constant: -20).isActive = true
            
    }
    
}

初始ViewController代码

import Foundation
import UIKit

class RecipeViewController: UIViewController {
    
    var selectedRecipe: Receipe?
    
    let recipeView:  RecipeUIView = {
        
        let view = RecipeUIView()
        view.translatesAutoresizingMaskIntoConstraints = false
        return view
    }()
    
    private lazy var scrollView: UIScrollView = {
        let scrollView = UIScrollView()
        scrollView.translatesAutoresizingMaskIntoConstraints = false
        return scrollView
    }()

    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        recipeView.setupView(currentRecipe: selectedRecipe)
        recipeView.directionalLayoutMargins = NSDirectionalEdgeInsets(top: 20, leading: 20, bottom: 20, trailing: 20)

        
        view.addSubview(scrollView)
        scrollView.addSubview(recipeView)
        
        let frameGuide = scrollView.frameLayoutGuide
        let contentGuide = scrollView.contentLayoutGuide
        
        
        // Scroll view layout guides (iOS 11)
        NSLayoutConstraint.activate([
            frameGuide.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            frameGuide.topAnchor.constraint(equalTo: view.topAnchor),
            frameGuide.trailingAnchor.constraint(equalTo: view.trailingAnchor),
            frameGuide.bottomAnchor.constraint(equalTo: view.bottomAnchor),

            contentGuide.leadingAnchor.constraint(equalTo: recipeView.leadingAnchor),
            contentGuide.topAnchor.constraint(equalTo: recipeView.topAnchor),
            contentGuide.trailingAnchor.constraint(equalTo: recipeView.trailingAnchor),
            contentGuide.bottomAnchor.constraint(equalTo: recipeView.bottomAnchor),

            contentGuide.widthAnchor.constraint(equalTo: frameGuide.widthAnchor),

            ])

    }

}

此时视图仍无法滚动,右侧没有滚动条。


更新:添加UITableView后不显示问题

文本现在可以滚动了,但在UIView中添加UITableView后,滚动功能正常但UITableView不显示,怀疑是约束问题,相关代码如下:

class RecipeUIView: UIView, UITableViewDelegate, UITableViewDataSource{

    var currentRecipe: Receipe?
    
    private let tableView: UITableView = {
        let tableView = UITableView()

        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "MyCell")
        
        tableView.backgroundColor = .green

        
        return tableView
    }()
    
    
    private var recipeTitle: UILabel! = {
        
        let label = UILabel()
        label.numberOfLines = 0
        label.font = .systemFont(ofSize: 24, weight: .bold)
        label.textColor = .gray
        label.textAlignment = .center
        label.translatesAutoresizingMaskIntoConstraints = false
        label.adjustsFontForContentSizeCategory = true
        
        return label
    }()
    

    override init(frame: CGRect) {
        super.init(frame: frame)
            }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        print("++++ IngrediantsTableViewCell tableview count: \(currentRecipe?.ingredients.count ?? 0)")
        
        return currentRecipe?.ingredients.count ?? 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            
        print("++++ IngrediantsTableViewCell tableview cellForRow ")

        
        let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell", for: indexPath as IndexPath)
            cell.textLabel!.text = "\(currentRecipe?.ingredients[indexPath.row].ingredient ?? "")"
            return cell

    }
    
    

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

        //return UITableView.automaticDimension
        
        return 30

    }
    
    
    func setupView(currentRecipe: Receipe?){
        
        let margin = readableContentGuide
        
        
        self.currentRecipe = currentRecipe

        recipeTitle.text = currentRecipe?.dynamicTitle


        addSubview(recipeTitle)


        // Constraints

        recipeTitle.topAnchor.constraint(equalTo: margin.topAnchor, constant: 4).isActive = true
        recipeTitle.leadingAnchor.constraint(equalTo: margin.leadingAnchor, constant: 20).isActive = true
        recipeTitle.trailingAnchor.constraint(equalTo: margin.trailingAnchor, constant: -20).isActive = true

        addSubview(tableView)
        tableView.delegate = self
        tableView.dataSource = self
        
        tableView.translatesAutoresizingMaskIntoConstraints = false
        tableView.topAnchor.constraint(equalTo: recipeTitle.bottomAnchor, constant: 10).isActive = true
        tableView.leadingAnchor.constraint(equalTo: margin.leadingAnchor, constant: 20).isActive = true
        tableView.trailingAnchor.constraint(equalTo: margin.trailingAnchor, constant: -20).isActive = true

        tableView.bottomAnchor.constraint(equalTo: margin.bottomAnchor, constant: -20).isActive = true

        tableView.reloadData()
                
    }
    
}

内容的提问来源于stack exchange,提问作者Siddharth Shekar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.22 12:54:23