【发布时间】: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 一样滚动太长
【问题讨论】:
标签: swift uiscrollview height