iosuialertcontroller

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.


Solution

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