【问题标题】:How to make scrollview scroll separately to its content height in swift如何让scrollview在swift中分别滚动到它的内容高度
【发布时间】:2021-08-06 09:31:12
【问题描述】:

我在故事板中有如下视图层次结构

这里用于内容主要约束top = 0, leading = 0, trailing = 0, bottom = 0

这里是 scrollview 约束top = 0, leading = 0, trailing = 0, bottom = 0

这里查看约束top = 0, leading = 0, trailing = 0, bottom = 0

对于 ContentView 约束 top = 0, leading = 0, trailing = 0

对于 TblReview 约束 top = 0, leading = 0, trailing = 0, bottom = 20

对于 Productcollectionview 约束 top = 0, leading = 0, trailing = 0, bottom = 20, height = 400

我在 swift 文件中有 Productcollectionview 高度出口,如下所示

我不想让collectionview单独滚动..我希望总视图根据collectionview单元格滚动..所以我写了下面的代码但是这个代码contentView and tblReview also scrolling upto productioncollectionview height我需要contentView 应该滚动到它的内容高度并且 tblReview 应该滚动到它的行

如何单独滚动到它的高度。

请帮我解决这个问题

  @IBOutlet weak var productCollHeight: NSLayoutConstraint!

 override func viewDidLoad() {
    super.viewDidLoad()
    
    productCollectionView.addObserver(self, forKeyPath: "contentSize", options: .new, context: nil)
} 
 override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
    let collectionView = object as? UICollectionView
    if collectionView == self.productCollectionView{
        if(keyPath == "contentSize"){
            if let newvalue = change?[.newKey]
            {
                let newsize  = newvalue as! CGSize
                self.productCollHeight.constant = newsize.height
            }
        }
    }
    }

@IBAction func aboutCompany(_ sender: UIButton){
self.productCollectionView.isHidden = true
self.tblReview.isHidden = true
self.contentView.isHidden = false
}

@IBAction func review(_ sender: UIButton){

self.productCollectionView.isHidden = true
self.tblReview.isHidden = false
self.contentView.isHidden = true
}

@IBAction func productOfSeller(_ sender: UIButton){
self.productCollectionView.isHidden = false
self.tblReview.isHidden = true
self.contentView.isHidden = true

}

我根据需要隐藏和显示 tblReview 和 contentView

使用上面的代码 tblReview 和 contentView 也滚动到 productCollectionView 高度..请帮我解决这个错误

编辑:share this seller 不在滚动视图中,所以这里 contentView 高度不是那么长,但是如果我滚动 contentView 也会像 productioncollectionview 一样滚动太长

这是 contentView,它像 productioncollectionview 一样滚动太久

【问题讨论】:

    标签: swift uiscrollview height


    【解决方案1】:

    如果您的所有约束都正确,而不仅仅是像这样使用它,您就不需要每次都计算。 UICollectionViewintrinsicContentSize 它会正确计数。

    final class ContentSizedCollectionView: UICollectionView {
        override var contentSize:CGSize {
            didSet {
                invalidateIntrinsicContentSize()
            }
        }
    
        override var intrinsicContentSize: CGSize {
            layoutIfNeeded()
            return CGSize(width: UIView.noIntrinsicMetric, height: contentSize.height)
        }
    }
    

    将该代码放入控制器或您想要放置的任何位置,并将ContentSizedCollectionView 类分配给您的collectionview。

    注意:您也可以在为UITableView 创建后将其与UITableView 一起使用。

    【讨论】:

    • 我在最后用图片编辑了我的帖子以了解..请帮忙
    猜你喜欢
    • 1970-01-01
    • 2014-08-15
    • 2012-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多