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

标签 ios swift xcode xib

首先,这是我的 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/

相关文章:

javascript - 仅限 iPad 上的 Adob​​e Edge 分层问题

ios - “线程1:断点3.5”错误

xcode - 在 Xcode 5 中禁用了 Clean Build Folder 选项

ios - 这两个部署目标有什么区别?

ios - 多个iTunes Connect帐户

ios - 为 UISlider 设置 ThumbImage 含糊不清

ios - 针对本地通知的个性化操作

swift - 如何实现 unarchiver 委托(delegate)来覆盖类名?

ios - 在 iOS 13 全屏中呈现模式

ios - Swift 通用回调