xib中的自定义xXib尺寸不正确

问题描述 投票:1回答:2

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

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

我遇到的麻烦是,我正在将最后一个视图(自定义视图)作为自定义xib加载到集合视图单元格中。由于某种原因,它的尺寸不正确:

enter image description here

如您所见,视图上方有一个巨大的空白,而不是正确地填充了单元格。有趣的是,如果我稍微向下移动表格视图,然后再向上滚动,它应该显示在它上面:

enter image description here

这是我的收藏夹查看代码的一部分:

// 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
}

这是我的集合视图单元格类,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 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:https://i.stack.imgur.com/GQLpj.png

MyCustomView xib:https://i.stack.imgur.com/jpSi0.png

所以我在这里做错了什么?

ios swift xcode xib
2个回答
0
投票

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

las 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() {
    //
}

}


0
投票

所以我找到了解决方案,但是我不知道它为什么起作用,以及它是否是解决它的最佳方法,所以我正在寻找输入。

如果我将我的PostMediaCollectionViewCell类修改为在初始化函数中包含layoutIfNeeded(),它将起作用:

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() {
        layoutIfNeeded()
    }
}

但是我不明白为什么。有人可以解释吗?

© www.soinside.com 2019 - 2024. All rights reserved.