[英]How can I create a segue form UIViewController to UICollectionViewController ? - Programmatically
Can I create an app where I do some stuff and then make a segue to UICollectionViewController?我可以创建一个应用程序来做一些事情,然后对 UICollectionViewController 进行 segue 吗?
Now a get this error: " Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'UICollectionView must be initialized with a non-nil layout parameter' "现在得到这个错误:“由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'UICollectionView 必须使用非零布局参数初始化'”
Of course, I can initiate it in SceneDelegate but I don't want to my UICollectionViewController was the first view.当然,我可以在 SceneDelegate 中启动它,但我不希望我的 UICollectionViewController 是第一个视图。
SceneDelegate when I use ViewController使用 ViewController 时的 SceneDelegate
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let scene = (scene as? UIWindowScene) else { return }
window = UIWindow(windowScene: scene)
window?.rootViewController = ViewController()
window?.makeKeyAndVisible()
}
SceneDelegate when i use UICollectionViewController当我使用 UICollectionViewController 时的 SceneDelegate
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let scene = (scene as? UIWindowScene) else { return }
window = UIWindow(windowScene: scene)
window?.makeKeyAndVisible()
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .horizontal
let navController = CollecionViewController(collectionViewLayout: layout)
window?.rootViewController = navController
}
this is my ViewController where I want to make a segue when I press the button这是我的 ViewController,当我按下按钮时,我想在其中进行转场
import UIKit
class ViewController: UIViewController { class 视图控制器:UIViewController {
var button = UIButton()
var button2 = UIButton()
var button3 : UIButton = {
let button = UIButton(type: .system)
button.setTitle("Push to 3 View Controller", for: .normal)
button.backgroundColor = .white
button.setTitleColor(.black, for: .normal)
button.frame = CGRect(x: 100, y: 300, width: 200, height: 50)
button.addTarget(self, action: #selector(didTap3Button), for: .touchUpInside)
return button
}()
@objc func didTap3Button() {
let rootVC = CollecionViewController()
let navVC = UINavigationController(rootViewController: rootVC)
present(navVC, animated: true)
}
You should do exactly the thing you did in the second code snippet in didTap3Button
, but instead of setting it as the rootViewController
, present
the navController
.您应该完全按照您在 didTap3Button 的第二个代码片段中所做的操作,但不要将其设置为didTap3Button
, rootViewController
present
navController
。 Your Collection VC need a layout.您的 Collection VC 需要一个布局。
@objc func didTap3Button() {
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .horizontal
let rootVC = CollecionViewController(collectionViewLayout: layout)
let navVC = UINavigationController(rootViewController: rootVC)
present(navVC, animated: true)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.