gpt4 book ai didi

ios - 另一个 xib 中的自定义 xib 大小不正确

转载 作者:行者123 更新时间:2023-12-01 16:20:55 26 4
gpt4 key购买 nike

首先,这是我的 View 层次结构:

- Table view
-- Table view cell (loaded from custom xib)
--- Collection view
---- Collection view cell (loaded from custom xib)
----- Custom view (loaded from custom xib)

我遇到的问题是最后一个 View ,即自定义 View ,我将其作为自定义 xib 加载到 Collection View 单元格中。由于某种原因,它的大小不正确:

enter image description here

如您所见, View 上方有一个巨大的空白区域,而不是正确填充单元格。有趣的是,如果我向下滚动表格 View ,然后向上滚动,它会显示在它应该:

enter image description here

这是我的收藏 View 代码的一部分:
// I use this to size the collection view cells to be of 16:9 ratio, nothing crazy
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
// Get the width of the screen
let width = UIScreen.main.bounds.width

let imageWidth = width
let imageHeight = imageWidth / (16 / 9)

self.postMediaHeight = imageHeight

return CGSize(width: imageWidth, height: imageHeight)
}

// Here I initialize the cell
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "PostMediaCollectionViewCell", for: indexPath as IndexPath) as? PostMediaCollectionViewCell else {
fatalError("The dequeued cell is not an instance of PostMediaCollectionViewCell.")
}

let media = post.images[indexPath.row]

cell.initialize()

return cell
}

这是我的 Collection View 单元类, PostMediaCollectionViewCell .没有什么不寻常的:
class PostMediaCollectionViewCell: UICollectionViewCell {

@IBOutlet weak var container: UIView!
@IBOutlet weak var imageView: UIImageView!
@IBOutlet weak var moreView: UIView!
@IBOutlet weak var moreViewLabel: UILabel!
@IBOutlet weak var myCustomView: MyCustomView!

override func awakeFromNib() {
super.awakeFromNib()
}

func initialize() {
//
}
}

最后,这是我的 MyCustomView 类(class):
class MyCustomView: UIView {
@IBOutlet var rootView: UIView!
@IBOutlet weak var videoView: VideoView!

override init(frame: CGRect) {
super.init(frame: frame)
initialize()
}

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
initialize()
}

func initialize() {
Bundle.main.loadNibNamed("MyCustomView", owner: self, options: nil)
addSubview(rootView)
rootView.frame = self.bounds
rootView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
}
}

我已经确保在我的 xib 中设置了我的约束,所以自动布局应该可以工作,但显然不是:

PostMediaCollectionViewCell xib:
/image/GQLpj.png

MyCustomView xib:
/image/jpSi0.png

那么我在这里做错了什么?

最佳答案

rootView.frame = self.bounds 行添加断点并检查 self.bounds值(value)观。如果值与滚动后的值不同,则应更改 awakeFromNib方法。

女士 PostMediaCollectionViewCell: UICollectionViewCell {

@IBOutlet weak var container: UIView!
@IBOutlet weak var imageView: UIImageView!
@IBOutlet weak var moreView: UIView!
@IBOutlet weak var moreViewLabel: UILabel!
@IBOutlet weak var myCustomView: MyCustomView!

override func awakeFromNib() {
super.awakeFromNib()

self.myCustomView.frame = self.bounds
self.myCustomView.autoresizingMask = [.flexibleWidth, .flexibleHeight]

}

func initialize() {
//
}

}

关于ios - 另一个 xib 中的自定义 xib 大小不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60254136/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com