UIAlertController + UIProgressView + macCatalyst

问题描述

我使用以下简单类在iOS应用中显示带有进度条的警报视图。没关系,但是当我尝试在为macOS构建的应用程序中使用相同的代码时,进度条不可见(请参阅所附图像)。

即使在macOS上也需要更改进度条吗?

Progress bar on iOS

Progress bar on 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,可以看到具有进度条的原始警报:

1

请记住,您不应该首先在警报控制器中添加自定义视图。引用docs

重要

UIAlertController类旨在按原样使用,而不是 支持子类化。此类的视图层次结构是私有的, 不得修改

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...