当在此UIAlertController上按下按钮时,它会自动关闭动画。我可以关闭动画吗?
我尝试过以动画形式呈现:虚假但仍然以动画效果解散。
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)
}
答案 0 :(得分:1)
首先我尝试的是创建一个UIAlertController
的引用来处理在处理程序中将dismiss(animated:completion:)
中的动画设置为false
(将在您之后执行的代码)按下UIAlertAction
的<确定按钮)
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)
}
}
不幸的是动画仍在那里:
对我有用的唯一方法就是override
dismiss(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)
}
}
现在警报视图在没有动画的情况下被解雇: