我创建的故事板我自己的窗口,如下图所示: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()
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.
}
}
在运行时,它给了我一个错误说:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'UICollectionView must be initialized with a non-nil layout parameter' *** First throw call stack:
在我的理解它要求我给布局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
}
但仍结束了一个错误:
Argument labels 'UICollectionViewLayout' do not match any available overloads.
我使用了错误的swift3语法?如果是的话解决这个问题是正确的。
为什么我不能使用let layout = UICollectionViewFlowLayout()。 UICollectionViewFlowLayout是UICollectionViewLayout的子类,用于实现简单网格的内置布局。布局高度可定制。 –
我只在答案中使用UICollectionViewFlowLayout。您使用的参数的名称是错误的。它应该是HomeController(collectionViewLayout:layout)而不是HomeController(UICollectionViewLayout:layout)。 –
对不起,我检查它,谢谢它的工作。 –