在iOS 13之前,展示了用于覆盖整个屏幕的视图控制器。并且,在关闭后,将执行父视图控制器viewDidAppear
函数。
现在,iOS 13默认将表单显示为视图控制器,这意味着卡将部分覆盖基础视图控制器,这意味着viewDidAppear
将不会被调用,因为父视图控制器从未真正消失过
是否可以检测到所显示的视图控制器工作表已被取消?我可以在父视图控制器中覆盖一些其他功能,而不是使用某种委托?
答案 0 :(得分:20)
设置了另一个获取viewWillAppear
和viewDidAppear
的选项
let vc = UIViewController()
vc.modalPresentationStyle = .fullScreen
此选项覆盖全屏,关闭后,调用上述方法
答案 1 :(得分:19)
这是父级视图控制器的代码示例,当子级视图控制器以表单形式呈现 (即以默认的iOS 13方式)时被通知:
public final class Parent: UIViewController, UIAdaptivePresentationControllerDelegate
{
// This is assuming that the segue is a storyboard segue;
// if you're manually presenting, just see the delegate there.
public override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
if segue.identifier == "mySegue" {
segue.destination.presentationController?.delegate = self;
}
}
public func presentationControllerDidDismiss(
_ presentationController: UIPresentationController)
{
// Only called when the sheet is dismissed by DRAGGING.
// You'll need something extra if you call .dismiss() on the child.
// (I found that overriding dismiss in the child and calling
// presentationController.delegate?.presentationControllerDidDismiss
// works well).
}
}
Jerland2的答案很混乱,因为(a)最初的提问者想在工作表被取消时得到一个函数调用(而他实现了presentationControllerDidAttemptToDismiss,当用户尝试时会被调用), 失败),并且(b)设置isModalInPresentation完全正交,实际上将使所呈现的表单无法被忽略(这与OP想要的相反)。
答案 2 :(得分:8)
对于将来的读者,这里是实施的更完整答案:
// Modal Dismiss iOS 13
modalNavController.presentationController?.delegate = modalVc
// MARK: - iOS 13 Modal (Swipe to Dismiss)
extension ModalViewController: UIAdaptivePresentationControllerDelegate {
func presentationControllerDidAttemptToDismiss(_ presentationController: UIPresentationController) {
print("slide to dismiss stopped")
self.dismiss(animated: true, completion: nil)
}
}
self.isModalInPresentation = true
答案 3 :(得分:5)
快速
在 iOS13
中拨打电话的常规解决方案viewWillAppear
class ViewController: UIViewController {
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
print("viewWillAppear")
}
//Show new viewController
@IBAction func show(_ sender: Any) {
let newViewController = NewViewController()
//set delegate of UIAdaptivePresentationControllerDelegate to self
newViewController.presentationController?.delegate = self
present(newViewController, animated: true, completion: nil)
}
}
extension UIViewController: UIAdaptivePresentationControllerDelegate {
public func presentationControllerDidDismiss( _ presentationController: UIPresentationController) {
if #available(iOS 13, *) {
//Call viewWillAppear only in iOS 13
viewWillAppear(true)
}
}
}
答案 4 :(得分:3)
是否可以检测到所显示的视图控制器工作表已被关闭?
是的
我可以在父视图控制器中重写某些其他功能,而不是使用某种委托?
不。 “某种代表”就是您的工作方式。使自己成为演示控制器的委托,并覆盖presentationControllerDidDismiss(_:)
。
答案 5 :(得分:3)
DRAG或CALL DISMISS FUNC将与以下代码一起使用。
1)在根视图控制器中,您将如下所示告诉它是其演示视图控制器
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "presenterID" {
let navigationController = segue.destination as! UINavigationController
if #available(iOS 13.0, *) {
let controller = navigationController.topViewController as! presentationviewcontroller
// Modal Dismiss iOS 13
controller.presentationController?.delegate = self
} else {
// Fallback on earlier versions
}
navigationController.presentationController?.delegate = self
}
}
2)再次在根视图控制器中,告诉您在使用其演示视图控制器时将要执行的操作
public func presentationControllerDidDismiss(
_ presentationController: UIPresentationController)
{
print("presentationControllerDidDismiss")
}
1)在演示视图控制器中,单击此图片中的“取消”或“保存”按钮。下面的代码将被调用。
self.dismiss(animated: true) {
self.presentationController?.delegate?.presentationControllerDidDismiss?(self.presentationController!)
}
答案 6 :(得分:3)
重写被关闭的UIViewController上的viewWillDisappear。它会通过isBeingDismissed
布尔标志来提醒您被解雇。
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
if isBeingDismissed {
print("user is dismissing the vc")
}
}
**如果用户在中途向下滑动并向后滑动卡,即使卡未被取消,该卡仍会注册为已被取消。但这是一个极端情况,您可能并不在意。
答案 7 :(得分:2)
如果您想在用户从该工作表中关闭模态工作表时执行某些操作。
假设您已经有一些带有 @IBAction
的关闭按钮和一个在关闭或执行其他操作之前显示警报的逻辑。您只想检测用户按下此类控制器的时刻。
方法如下:
class MyModalSheetViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.presentationController?.delegate = self
}
@IBAction func closeAction(_ sender: Any) {
// your logic to decide to close or not, when to close, etc.
}
}
extension MyModalSheetViewController: UIAdaptivePresentationControllerDelegate {
func presentationControllerShouldDismiss(_ presentationController: UIPresentationController) -> Bool {
return false // <-prevents the modal sheet from being closed
}
func presentationControllerDidAttemptToDismiss(_ presentationController: UIPresentationController) {
closeAction(self) // <- called after the modal sheet was prevented from being closed and leads to your own logic
}
}
答案 8 :(得分:1)
在 SwiftUI 中你可以使用 onDismiss 闭包
func sheet<Item, Content>(item: Binding<Item?>, onDismiss: (() -> Void)?, content: (Item) -> Content) -> some View
答案 9 :(得分:-1)
如果某人无权访问所显示的视图控制器,则他们可以在呈现视图控制器时重写以下方法,并将modalPresentationStyle
更改为fullScreen
或可以添加上述策略之一用这种方法
override func present(_ viewControllerToPresent: UIViewController, animated flag: Bool, completion: (() -> Void)? = nil) {
if let _ = viewControllerToPresent as? TargetVC {
viewControllerToPresent.modalPresentationStyle = .fullScreen
}
super.present(viewControllerToPresent, animated: flag, completion: completion)
}
如果显示的视图控制器是导航控制器,并且您要检查根控制器,则可以将上述条件更改为
if let _ = (viewControllerToPresent as? UINavigationController)?.viewControllers.first as? TargetVC {
viewControllerToPresent.modalPresentationStyle = .fullScreen
}
答案 10 :(得分:-2)
从我的角度来看,Apple不应该将pageSheet
设置为默认的modalPresentationStyle
我想通过使用fullScreen
swizzling
样式恢复为默认样式
赞:
private func _swizzling(forClass: AnyClass, originalSelector: Selector, swizzledSelector: Selector) {
if let originalMethod = class_getInstanceMethod(forClass, originalSelector),
let swizzledMethod = class_getInstanceMethod(forClass, swizzledSelector) {
method_exchangeImplementations(originalMethod, swizzledMethod)
}
}
extension UIViewController {
static func preventPageSheetPresentationStyle () {
UIViewController.preventPageSheetPresentation
}
static let preventPageSheetPresentation: Void = {
if #available(iOS 13, *) {
_swizzling(forClass: UIViewController.self,
originalSelector: #selector(present(_: animated: completion:)),
swizzledSelector: #selector(_swizzledPresent(_: animated: completion:)))
}
}()
@available(iOS 13.0, *)
private func _swizzledPresent(_ viewControllerToPresent: UIViewController,
animated flag: Bool,
completion: (() -> Void)? = nil) {
if viewControllerToPresent.modalPresentationStyle == .pageSheet
|| viewControllerToPresent.modalPresentationStyle == .automatic {
viewControllerToPresent.modalPresentationStyle = .fullScreen
}
_swizzledPresent(viewControllerToPresent, animated: flag, completion: completion)
}
}
然后将此行放入您的AppDelegate
UIViewController.preventPageSheetPresentationStyle()
答案 11 :(得分:-2)
如果您在FullScreen中使用ModalPresentationStyle,则控制器的行为将恢复为正常状态。
ConsultarController controllerConsultar = this.Storyboard.InstantiateViewController(“ ConsultarController”)作为ConsultarController; controllerConsultar.ModalPresentationStyle = UIModalPresentationStyle.FullScreen; this.NavigationController.PushViewController(controllerConsultar,true);
答案 12 :(得分:-4)
调用presentingViewController.viewWillAppear会不会很简单? 是因为解雇?
self.presentingViewController?.viewWillAppear(false)
self.dismiss(animated: true, completion: nil)