UIAlertController,在关闭时消除动画?

时间:2017-06-21 11:47:23

标签: ios uialertcontroller

这是一个简单的操作表,

let choice = UIAlertController(title: "Choose", message: nil, preferredStyle: .actionSheet)

choice.addAction(UIAlertAction(title: "Camera", style: .default, handler: { _ in
        self.happyCamera() }))

choice.addAction(UIAlertAction(title: "Album", style: .default, handler: { _ in
        self.happyAlbum() }))

choice.addAction(UIAlertAction.init(title: "Cancel", style: .cancel, handler: nil))

somewhere?.present(choice, animated: false, completion: nil)

当操作表出现时(注意当前#animated为false),它只是点击屏幕,没有干酪动画。

但是,当用户点击三个选项中的一个时,或者点击“关闭”时,操作表将使用奶酪动画离开屏幕。

(特别是在10.3中,它从屏幕向下滑动。)

有没有办法关闭退出动画?

enter image description here

如果您继承UIAlertController ...它不起作用?

如下面的DS所示,您可以继承UIAlertController。

然而 - 奇怪的是 - 它什么也没做。这是一个测试

func _test() {

    let msg = SuperiorUIAlertController(
        title: "Hello", message: "Hello",
        preferredStyle: UIAlertControllerStyle.alert)
    msg.addAction(UIAlertAction(
        title: "OK", style: UIAlertActionStyle.default,
        handler: nil))

    let win = UIWindow(frame: UIScreen.main.bounds)
    let vc = UIViewController()
    vc.view.backgroundColor = .clear
    win.rootViewController = vc
    win.windowLevel = UIWindowLevelAlert + 1
    win.makeKeyAndVisible()    
    vc.present(msg, animated: false, completion: nil)
}

class SuperiorUIAlertController: UIAlertController {

    override func dismiss(animated flag: Bool, completion: (() -> Void)? = nil) {

        print("You should see this! \(flag)")
        self.dismiss(animated: false, completion: completion)
    }
}

事实上,文字“你应该看到这个”永远不会出现

2 个答案:

答案 0 :(得分:1)

我不想回答我自己的问题,但截至2017年底,没有办法。奇怪吧?

希望这个事实有助于某人。

答案 1 :(得分:0)

还有一种方法可以覆盖viewController的dismiss方法。如果您不想覆盖其他动画,请检查动画标记值或在下面的方法中制作标记。

全局制作AlertController

var choice = UIAlertController()   

确保在viewController中添加此方法,并显示警告

在没有动画的情况下解除提示警报,如下所示

override func dismiss(animated flag: Bool, completion: (() -> Void)? = nil) {
    self.choice.dismiss(animated: false, completion: nil)
}