从 collectionView 中呈现 viewController
Present a viewController from collectionView
我正在扩展 UICollectionView 以创建一个空视图,我无法调用 self.present。有没有更好的方法来处理空视图?我应该如何处理 collectionView 中的 self.present?
extension UICollectionView {
func setEmptyView() {
let signUpButton = UIButton(type: .custom)
signUpButton.addTarget(self, action: #selector(onSignIn), for: .touchUpInside)
}
@objc func onSignIn() {
let viewController = SignInController()
viewController.modalPresentationStyle = .fullScreen
}
}
您可以从顶级控制器展示..使用此扩展程序您将获得顶级控制器...
extension UIApplication {
class func getTopMostViewController() -> UIViewController? {
let keyWindow = UIApplication.shared.windows.filter {[=10=].isKeyWindow}.first
if var topController = keyWindow?.rootViewController {
while let presentedViewController = topController.presentedViewController {
topController = presentedViewController
}
return topController
} else {
return nil
}
}
}
然后在您的collection视图扩展中
extension UICollectionView {
func setEmptyView() {
let signUpButton = UIButton(type: .custom)
signUpButton.addTarget(self, action: #selector(onSignIn), for: .touchUpInside)
}
@objc func onSignIn() {
let viewController = SignInController()
viewController.modalPresentationStyle = .fullScreen
UIApplication.getTopMostViewController()?.present(viewController, animated: true, completion: nil)
}
}
我正在扩展 UICollectionView 以创建一个空视图,我无法调用 self.present。有没有更好的方法来处理空视图?我应该如何处理 collectionView 中的 self.present?
extension UICollectionView {
func setEmptyView() {
let signUpButton = UIButton(type: .custom)
signUpButton.addTarget(self, action: #selector(onSignIn), for: .touchUpInside)
}
@objc func onSignIn() {
let viewController = SignInController()
viewController.modalPresentationStyle = .fullScreen
}
}
您可以从顶级控制器展示..使用此扩展程序您将获得顶级控制器...
extension UIApplication {
class func getTopMostViewController() -> UIViewController? {
let keyWindow = UIApplication.shared.windows.filter {[=10=].isKeyWindow}.first
if var topController = keyWindow?.rootViewController {
while let presentedViewController = topController.presentedViewController {
topController = presentedViewController
}
return topController
} else {
return nil
}
}
}
然后在您的collection视图扩展中
extension UICollectionView {
func setEmptyView() {
let signUpButton = UIButton(type: .custom)
signUpButton.addTarget(self, action: #selector(onSignIn), for: .touchUpInside)
}
@objc func onSignIn() {
let viewController = SignInController()
viewController.modalPresentationStyle = .fullScreen
UIApplication.getTopMostViewController()?.present(viewController, animated: true, completion: nil)
}
}