我的代码是:
import UIKit
class RCSubscriptionPackageView: UIView {
let centeredCollectionViewFlowLayout = CenteredCollectionViewFlowLayout()
let collectionView :UICollectionView
let cellPerWidth = 0.7
let container: UIView!
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
collectionView = UICollectionView(centeredCollectionViewFlowLayout: centeredCollectionViewFlowLayout)
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
}
错误在上面的初始化程序
中override init(frame: CGRect) {
super.init(frame: frame)
self.backgroundColor = .lightGray
collectionView.backgroundColor = .clear
centeredCollectionViewFlowLayout.itemSize = CGSize(
width: self.bounds.width * CGFloat(cellPerWidth),
height: self.bounds.height * CGFloat(cellPerWidth) * CGFloat(cellPerWidth)
)
centeredCollectionViewFlowLayout.minimumLineSpacing = 20
collectionView.showsVerticalScrollIndicator = false
collectionView.showsHorizontalScrollIndicator = true
addContainerView()
addConstraintsForContainer()
collectionView.register(
RCSubscriptionPackCollectionViewCell.self,
forCellWithReuseIdentifier: String(describing: RCSubscriptionPackCollectionViewCell.self)
)
centeredCollectionViewFlowLayout.minimumLineSpacing = 20
collectionView.showsVerticalScrollIndicator = false
collectionView.showsHorizontalScrollIndicator = true
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func addContainerView() -> Void {
container.backgroundColor = .lightGray
container.translatesAutoresizingMaskIntoConstraints = false
container.addSubview(collectionView)
}
func addConstraintsForContainer() -> Void {
container.snp.makeConstraints { (make) in
make.edges.equalToSuperview()
}
}
}
错误表示'初始化程序不会覆盖超类中的指定初始化程序'
并在
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
错误是 '无法调用' UIView.init'使用类型'的参数列表(nibName:String ?, bundle:Bundle?)'
我在每个地方都搜索过它,但没有得到任何东西。 请让我离开这个。
答案 0 :(得分:0)
UIView
支持这两个初始化程序
init(frame: CGRect)
init?(coder aDecoder: NSCoder)
init(nibName:bundle:
属于视图控制器或NIB