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

问题描述 投票:2回答:2

我是新的iOS编程,现在我很着迷使用谷歌提供的MaterialComponents。现在我在名为Dialog的组件中面临一个问题。

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

我不希望用户单击外部弹出视图以关闭该弹出视图。我想要的只是希望用户点击我提供给用户选择的操作按钮,然后只有点击该操作按钮才能解除视图。

真的很高兴你帮忙。

ios swift uialertview material-components-ios
2个回答
1
投票

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
    })

© www.soinside.com 2019 - 2024. All rights reserved.