我正在尝试加载 nib 文件以在 collectionViewController 内作为视差标题查看,但在加载视图后,视图的宽度为 0.0,并且没有显示。如果我加载任何其他视图,宽度还可以,但此视图的宽度会发生变化加载后归零。这是我的代码。
class CastHeader: UIView{
private func commonInit() {
guard let view = Bundle.main.loadNibNamed("CastHeader", owner: self, options: nil)![0] as? UIView else{return}
addSubview(view)
view.translatesAutoresizingMaskIntoConstraints = false
let bindings = ["view": view]
addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[view]|", options:[], metrics:nil, views: bindings))
addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[view]|", options:[], metrics:nil, views: bindings))
}
// MARK: User interface
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
awakeFromNib()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
commonInit()
awakeFromNib()
}
}
对此有什么意见吗?先感谢您