我在故事板中创建了自己的窗口,如下所示:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
window = UIWindow(frame: UIScreen.main.bounds)
window?.makeKeyAndVisible()
window?.rootViewController = UINavigationController(rootViewController: HomeController())
return true
}
当我的类继承 UIViewController时,这实际上工作正常。
class HomeController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
view.backgroundColor = UIColor.red
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
现在我用 UICollectionViewController 继承我的类:
class HomeController: UICollectionViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
Collectionview.backgroundColor = UIColor.red
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
在运行时它给我一个错误说:
由于未捕获的异常而终止应用 'NSInvalidArgumentException',原因:'UICollectionView必须是 使用非零布局参数初始化' ***首先抛出调用堆栈:
根据我的理解,它要求我为 UICollectionView 提供布局。如果我错了,请在这里告诉我。所以我试过了 - :
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
window = UIWindow(frame: UIScreen.main.bounds)
window?.makeKeyAndVisible()
let layout = UICollectionViewFlowLayout()
window?.rootViewController = UINavigationController(rootViewController: HomeController(UICollectionViewLayout: layout))
return true
}
但仍然以错误结束:
参数标签'UICollectionViewLayout'与任何可用的都不匹配 过载。
我使用了错误的 swift3语法吗?如果是,那么解决这个问题是正确的。
答案 0 :(得分:2)
您需要使用UICollectionViewController
指定的intialiser,如下所示。
let layout = UICollectionViewLayout()
let homeViewController = HomeController(collectionViewLayout: layout)
window?.rootViewController = UINavigationController(rootViewController: homeViewController)
除此之外,您还需要修改HomeViewController
以实施UICollectionViewDataSource
所需的方法。
override func viewDidLoad() {
super.viewDidLoad()
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Register cell classes
self.collectionView!.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "Cell")
// Do any additional setup after loading the view.
}
// MARK: UICollectionViewDataSource
override func numberOfSections(in collectionView: UICollectionView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 0
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of items
return 0
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath)
// Configure the cell
return cell
}
答案 1 :(得分:1)
您需要在UICollectionViewFlowLayout
中实施UICollectionViewController
。
选中此Link
您必须使用initWithFrame:collectionViewLayout:
初始化UICollectionView
,并且必须传递非零UICollectionViewLayout object
。