ios - UIScrollView 高度不根据 View 大小动态更新?

标签 ios swift uitableview uiscrollview constraints

我有 VC,因为我添加了 UIScrollView -> UIView。之后我添加了 3 个 tableViews 并添加了约束。基于动态调整大小的内容 TableView ,例如 tbl1、tbl2 和 tbl3 分别有 10、5 和 7 行。根据内容,我想更新我的整个 View 。这里 tableViews height 更新成功但不是 ScrollView

我的代码是...

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.

    tbl1.delegate = self
    tbl1.dataSource = self
    tbl2.delegate = self
    tbl2.dataSource = self
    tbl3.delegate = self
    tbl3.dataSource = self
}

override func viewDidLayoutSubviews() {

    DispatchQueue.main.async {
        //Update tbl1 frame
        var frame = self.tbl1.frame
        frame.size.height = self.tbl1.contentSize.height
        self.tbl1.frame = frame
        //Update tbl2 frame
        var frame2 = self.tbl2.frame
        let y = self.tbl1.frame.maxY
        frame2.origin.y = y
        frame2.size.height = self.tbl2.contentSize.height
        self.tbl2.frame = frame2
        //Update tbl3 frame
        var frame3 = self.tbl3.frame
        let y2 = self.tbl2.frame.maxY
        frame3.origin.y = y2
        frame3.size.height = self.tbl3.contentSize.height
        self.tbl3.frame = frame3
    }

}

// MARK: - UITableView Delegates
func numberOfSections(in tableView: UITableView) -> Int {

    return 1
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if tableView == tbl1 {
        return 10
    } else if tableView == tbl2 {
        return 5
    } else {
        return 7
    }
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if tableView == tbl1 {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        cell.textLabel?.text = "TBL ONE"

        return cell
    } else if tableView == tbl2 {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        cell.textLabel?.text = "TBL TWO"

        return cell
    } else {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        cell.textLabel?.text = "TBL THREE"

        return cell
    }

}

最佳答案

只需将此行添加到 viewDidLayoutSubviews() 的末尾即可

self.scrollView.contentSize.height = self.tbl1.contentSize.height + self.tbl2.contentSize.height + self.tbl3.contentSize.height

关于ios - UIScrollView 高度不根据 View 大小动态更新?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53096268/

相关文章:

ios - 关闭模态,然后立即推送 View Controller

swift - NSNull 转换为具有 NSDate 类型属性的 Struct

iphone - SWIFT 委托(delegate) finishedWithAuth

iphone - 在自定义 UITableViewCell 中访问 UITextField

ios - 在 storyBoard 中的 iOS tableView 单元格中设置相对于屏幕高度的 collectionView 高度

ios - meteor Cordova 构建与生产服务器(无热代码推送)

iPhone 应用程序在 [NSString stringWithFormat] 崩溃

ios - iOS 中通用链接和旧 URL 方案之间的区别

ios - Iphone 模拟器无法定位

swift - 当我搜索搜索 Controller 时,indexPath 不会随原始数组改变