Fattie
Fattie

Reputation: 12648

UIAlertController, eliminate the animation on closing?

Here's a simple action sheet,

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)

When the action sheet appears (note that present#animated is false) it just clicks on to the screen, no cheesey animation.

However, when the user taps one of the three choices, or, taps "off", the action sheet leaves the screen by using the cheesey animation.

(In 10.3 specifically, it slides downwards off the screen.)

Is there a way to turn off that exit animation?

enter image description here


If you subclass UIAlertController...it doesn't work?

As DS suggests below, you could subclass UIAlertController.

However - strangely - it does nothing. Here's a test

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

Indeed, the text "You should see this" never appears.

Upvotes: 2

Views: 714

Answers (3)

Fattie
Fattie

Reputation: 12648

I hate to answer my own question, but as of late 2017, there is no way. Weird right?

Upvotes: 2

Janneman
Janneman

Reputation: 1223

Try subclassing UIAlertController like this:

class InstantCloseAlertController: UIAlertController {
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        UIView.setAnimationsEnabled(false)
    }
    
    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
        UIView.setAnimationsEnabled(true)
    }
}

Upvotes: 0

Dharma
Dharma

Reputation: 3013

The one more way you can Override dismiss method of viewController. If you don't wanna override other animations check animated flag value or make a flag in below method.

Make your AlertController globally

var choice = UIAlertController()   

Make sure add this method in your viewController which you are presented alert

Dismiss presented alert without animation like below

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

Upvotes: 0

Related Questions