iOS、Swift、UIAlertController、UIAlertAction - 如何在按下按钮时关闭关闭动画?
iOS, Swift, UIAlertController, UIAlertAction - How to turn dismiss animation off when button is pressed?
当按下此 UIAlertController 上的按钮时,它会自动关闭并显示动画。我可以关闭动画吗?
我已经尝试呈现为动画 : 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)
}
首先我尝试的是,创建一个 UIAlertController
的引用来处理在处理程序中将 dismiss(animated:completion:)
中的动画设置为 false
(将要执行的代码在你按下 UIAlertAction
:
的 OK 按钮之后
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)
}
}
现在警报视图在没有动画的情况下被关闭:
当按下此 UIAlertController 上的按钮时,它会自动关闭并显示动画。我可以关闭动画吗?
我已经尝试呈现为动画 : 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)
}
首先我尝试的是,创建一个 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)
}
}
现在警报视图在没有动画的情况下被关闭: