如何解决UIAlertController + UIProgressView + macCatalyst
我使用以下简单类在iOS应用中显示带有进度条的警报视图。没关系,但是当我尝试在为macOS构建的应用程序中使用相同的代码时,进度条不可见(请参阅所附图像)。
即使在macOS上也需要更改进度条吗?
protocol ProgressDelegate: class {
func onProgressCanceled()
}
class ProgressAlert {
private let alert: UIAlertController
private var progressBar: UIProgressView
init(title: String,delegate: ProgressDelegate?) {
alert = UIAlertController(title: title,message: "",preferredStyle: .alert)
progressBar = UIProgressView(progressViewStyle: .default)
progressBar.tintColor = Theme.appColor
alert.addAction(UIAlertAction(title: "Cancel",style: .cancel) { alertAction in
delegate?.onProgressCanceled()
})
}
func present(from uivc: UIViewController) {
uivc.present(alert,animated: true,completion: {
let margin: CGFloat = 16.0
let rect = CGRect(x: margin,y: 56.0,width: self.alert.view.frame.width - margin * 2.0,height: 2.0)
self.progressBar.frame = rect
self.alert.view.addSubview(self.progressBar)
})
}
func dismiss(completion: (() -> Void)?) {
alert.dismiss(animated: true,completion: completion)
}
func setProgress(_ value: Float) {
progressBar.setProgress(value,animated: true)
print("Updating download: \(value)")
}
}
解决方法
实际上已按预期添加了进度条,但Mac Catalyst完全隐藏了UIAlertController
视图,并显示了本地macOS NSAlert
。
通过将alert.view.isHidden
设置为false
,可以看到具有进度条的原始警报:
请记住,您不应该首先在警报控制器中添加自定义视图。引用docs:
重要
UIAlertController类旨在按原样使用,而不是 支持子类化。此类的视图层次结构是私有的, 不得修改。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。