ios - UIAlertController + UIProgressView + macCatalyst

标签 ios swift macos mac-catalyst

我使用以下简单类在我的 iOS 应用程序中显示带有进度条的警报 View 。没关系,但是当我尝试在为 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
请记住,您一开始就不应该向警报 Controller 添加自定义 View 。引自 docs :

Important

The UIAlertController class is intended to be used as-is and does not support subclassing. The view hierarchy for this class is private and must not be modified.

关于ios - UIAlertController + UIProgressView + macCatalyst,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63509246/

相关文章:

linux - 递归目录时 Bash 内存 "leak"

android - 内置加密 API 的不同操作系统

android - Ionic/Cordova - AJAX 请求失败,状态代码为 -1

ios - 离开 GameScene 时停止 SpriteKit 中的音乐

Swift Beta 性能 : sorting arrays

swift - 快速将正值转换为负值

android - 如何在 Swift 中将 protobuf 对象转换为 ByteArray 并使用 Base64 URL_SAFE 进行编码?

ios - RSS 提要无法加载

ios - 应用程序在前台时是否可以不触发本地通知?

java - 无法从 build.gradle 中的 System.getenv() 访问 mac 上的环境变量