Swift3 内容视图调整为父滚动视图

Swift3 内容视图调整为父滚动视图,swift3,uiscrollview,Swift3,Uiscrollview,我的滚动视图有很多组件的内容视图,这些组件在单击时正在制作动画。有一个问题,内容视图没有正确更改大小。有人知道怎么解决这个问题吗?在滚动它后面的CONNT视图的大小时。下面是一段代码,描述了我正在做的事情: func updateDropDownList(tableView: UITableView, height: CGFloat, state: CGFloat) { let rowHeight = tableView.rowHeight UIView.ani

我的滚动视图有很多组件的内容视图,这些组件在单击时正在制作动画。有一个问题,内容视图没有正确更改大小。有人知道怎么解决这个问题吗?在滚动它后面的CONNT视图的大小时。下面是一段代码,描述了我正在做的事情:

 func updateDropDownList(tableView: UITableView, height: CGFloat, state: CGFloat) {
        let rowHeight = tableView.rowHeight
        UIView.animate(withDuration: 0.3, animations: {
            var frame = CGRect(x: tableView.frame.origin.x, y: tableView.frame.origin.y, width: tableView.frame.width, height: height)
            tableView.frame = frame
            if state == -1 {
                frame = CGRect(x: tableView.frame.origin.x, y: tableView.frame.origin.y, width: tableView.frame.width, height: CGFloat(tableView.numberOfRows(inSection: 0)) * rowHeight)
            }
            self.updatePositions(tableView: tableView, frame: frame, state: state, rowHeight: rowHeight)
        })
    }
声明:

  • state=1//调整表视图的大小
  • state=-1//返回原始大小
  • 以及允许我更改每个组件位置的方法:

    func updatePositions(tableView: UITableView, frame: CGRect, state: CGFloat, rowHeight: CGFloat) {
            switch tableView {
            case facilitiesTableView:
                statusLabel.frame.origin.y += state * (frame.height - rowHeight)
                statusTableView.frame.origin.y += state * (frame.height - rowHeight)
                serviceTableView.frame.origin.y += state * (frame.height - rowHeight)
                vehiclesCollection.frame.origin.y += state * (frame.height - rowHeight)
                colorsCollection.frame.origin.y += state * (frame.height - rowHeight)
                scrollView.contentSize.height += state * colorsCollection.frame.height
    //            contentView.frame.size = CGSize(width: contentView.frame.size.width, height: scrollView.contentSize.height)
            case statusTableView:
                serviceTableView.frame.origin.y += state * (frame.height - rowHeight)
                vehiclesCollection.frame.origin.y += state * (frame.height - rowHeight)
                colorsCollection.frame.origin.y += state * (frame.height - rowHeight)
                scrollView.contentSize.height += state * colorsCollection.frame.height
            case serviceTableView:
                vehiclesCollection.frame.origin.y += state * (frame.height - rowHeight)
                colorsCollection.frame.origin.y += state * (frame.height - rowHeight)
                scrollView.contentSize.height += state * colorsCollection.frame.height
            default:
                break
            }
        }
    
    有一个注释行可以调整内容视图的大小,但当我调用它时,它会弄乱我的屏幕,单击的表不会调整大小,因为内容视图中有这些组件,如果我没有错的话,它们会重新绘制它们

    如果没有调整大小的内容视图以适应滚动视图,我无法单击最低的组件,因为它们超出了内容视图的范围


    提前谢谢

    在我的观察中,这些问题是由于约束未随动画一起更新而出现的。 尝试在动画完成前后添加以下代码行

    contentView.layoutIfNeeded()
    

    这一小行代码在很多场合对我都有帮助。希望它对你有用

    如果我在动画之前添加它,它不会有任何帮助,如果我在动画之后添加它,它也不会让我点击表格视图的任何部分-它不会让你使用任何布局约束?我是。与滚动视图的顶部、底部、前导和尾部一样,每个视图都是0,高度和宽度与滚动视图的superview相等。