UITableView within UIScrollView using autolayout

前端 未结 5 1107
渐次进展
渐次进展 2020-11-28 18:07

At the moment, I\'m using a UITableView along with other views that are contained in a UIScrollView. I want the UITableView to have it

5条回答
  •  遥遥无期
    2020-11-28 18:41

    In addition to rob's answer there is swift example of self-resizable subclass of UITableView:

    Swift 2.x

    class IntrinsicTableView: UITableView {
    
        override var contentSize:CGSize {
            didSet {
                self.invalidateIntrinsicContentSize()
            }
        }
    
    
        override func intrinsicContentSize() -> CGSize {
            self.layoutIfNeeded()
            return CGSizeMake(UIViewNoIntrinsicMetric, contentSize.height)
        }
    
    }
    

    Swift 3.x or Swift 4.x

    class IntrinsicTableView: UITableView {
    
        override var contentSize:CGSize {
            didSet {
                self.invalidateIntrinsicContentSize()
            }
        }
    
        override var intrinsicContentSize: CGSize {
            self.layoutIfNeeded()
            return CGSize(width: UIViewNoIntrinsicMetric, height: contentSize.height)
        }
    
    }
    

    I have used it to put a table view into another auto-resizable table view's cell.

提交回复
热议问题