2017-02-04 3 views
1

このUIAlertControllerでボタンを押すと自動的にアニメーションが解除されます。アニメーションをオフにすることはできますか?iOS、Swift、UIAlertController、UIAlertAction - ボタンを押したときにアニメーションをオフにするにはどうすればいいですか?

私はアニメーションとして表示しようとしました:falseしかし、まだアニメーションで却下します。

 func showOKMessage(title: String, message : String) { 
     self.alertController = UIAlertController(title: title, message: message, preferredStyle: .alert) 
     let okAction = UIAlertAction(title: "OK", style: .default) 
     self.alertController.addAction(okAction) 
     self.present(self.alertController, animated: true) 
    } 
+1

あなたは、コードを追加することができますしてください – Ram

+0

コードをシンプルなものにしてください –

+0

私はボタンを無効にするのがよい回避策であると私は思っています。 –

答えて

1

私はUIAlertActionのハンドラ(あなたはOKボタンを押した後に実行されるコード)にfalsedismiss(animated:completion:)にアニメーションを設定するために処理するUIAlertControllerの参照を作成するために、ISしようとしたものを第一に:

import UIKit 

class ViewController: UIViewController { 

    var alert: UIAlertController! 

    @IBAction func alertViewButtonPressed(_ sender: UIButton) { 
    alert = UIAlertController(title: "", message: "Hello", preferredStyle: .alert) 
    let action = UIAlertAction(title: "OK", style: .default) { _ in 
     // this code executes after you hit the OK button 
     self.alert.dismiss(animated: false, completion: nil) 
    } 
    alert.addAction(action) 
    self.present(alert, animated: true) 
    } 
} 

は残念ながらアニメーションがまだある:

enter image description here

唯一の方法は、overridedismiss(animated:completion:)メソッドにあり、superからfalseの呼び出しでアニメーションフラグを設定することです。ハンドラにコードを追加する必要もなく、そのソリューションの参照を作成する必要はありません。 (注:今すぐすべての提示ビューコントローラは、そのビューコントローラでアニメーションせずに却下されます):

import UIKit 

class ViewController: UIViewController { 

    @IBAction func alertViewButtonPressed(_ sender: UIButton) { 
    let alert = UIAlertController(title: "", message: "Hello", preferredStyle: .alert) 
    let action = UIAlertAction(title: "OK", style: .default, handler: nil) 
    alert.addAction(action) 
    self.present(alert, animated: true) 
    } 

    override func dismiss(animated flag: Bool, completion: (() -> Void)? = nil) { 
    // view controller which was presented modally by the view controller gets dismissed now without animation 
    super.dismiss(animated: false, completion: completion) 
    } 
} 

今のアラートビューは、アニメーションなしで却下されます:

enter image description here

+0

アラートビューごとに回避策を提供することはできますか? –

関連する問題