首先是我的尝试是,创建一个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)
}
}
现在警报视图被解雇而无需动画:
可以添加代码,请 – Ram
代码请sims像一个简单的修复 –
在simliar情况下,我发现禁用该按钮是一个很好的解决方法,我不知道这将有助于你的情况没有更多的信息,虽然。 –