如何使用 MDCAlertController Material 设计禁用屏幕外触摸关闭视图 - swift

How to disable touch outside screen dismiss view using MDCAlertController Material Design - swift

我是 iOS 编程新手,现在着迷于使用 google 提供的 MaterialComponents。现在我在名为 Dialog 的组件中遇到一个问题。

当我触摸该弹出视图外部时,该视图已在屏幕上弹出,然后该视图已被关闭。我不希望这种情况发生在我的应用程序中。

我不希望用户在弹出视图外部单击以关闭该弹出视图。我想要的只是希望用户单击我提供给用户选择的操作按钮,然后仅在单击该操作按钮时应关闭视图。

很高兴你能提供帮助。

MDCAlertController 继承自 UIViewController.

因此,为了限制用户在 MDCAlertController 外部单击,您必须访问名为 view 的 属性,然后是 superview?.subviews[0].isUserInteractionEnabled = false

我已经使用 MDCAlertController 完成了一个示例

let alert = MDCAlertController(title: title, message: message)

    alert.buttonTitleColor = UIColor(red:0.03, green:0.62, blue:0.09, alpha:1.0)

    //MDCAlertControllerThemer.applyScheme(alertScheme, to: alert)
    let okayAction = MDCAlertAction(title: "Okay") { (action) in

        print("User click okay")

    }
    let cancelAction = MDCAlertAction(title: "Cancel", handler: nil)
    alert.addAction(okayAction)
    alert.addAction(cancelAction)

    self.present(alert, animated: true, completion: {

        // When the Dialog view has pop up on screen then just put this line of code when Dialog view has completed pop up.
        alert.view.superview?.subviews[0].isUserInteractionEnabled = false
    })

使用这个。

let alert = MDCAlertController(title: title, message: message)

alert.mdc_dialogPresentationController.dismissOnBackgroundTap = false

https://material.io/develop/ios/components/dialogs/api-docs/Categories/UIViewController_28MaterialDialogs_29.html

https://material.io/develop/ios/components/dialogs/api-docs/Classes/MDCDialogPresentationController.html#/c:objc(cs)MDCDialogPresentationController(py)dismissOnBackgroundTap