【问题标题】:UIAlertController + UIProgressView + macCatalystUIAlertController + UIProgressView + macCatalyst
【发布时间】:2020-12-10 00:52:42
【问题描述】:

我使用以下简单类在我的 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)")
}
}

【问题讨论】:

    标签: ios swift macos mac-catalyst


    【解决方案1】:

    进度条实际上是按预期添加的,但是 Mac Catalyst 完全隐藏了 UIAlertController 视图,而是呈现了一个原生 macOS NSAlert

    您可以通过将alert.view.isHidden 设置为false 来查看带有进度条的原始警报:

    请记住,您一开始就不应该向警报控制器添加自定义视图。引用docs:

    重要

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

    【讨论】:

    • 感谢您的回答。显示带有进度条的可取消对话的最佳做法是什么?
    • 没有我所知道的 iOS 原生解决方案。在 macOS 上,您可以使用 NSAlert 和进度指示器作为 accessoryView。但如果你想要一个适用于 iOS 和 macOS 的通用解决方案,我想自定义对话框是可行的方法。
    猜你喜欢
    • 1970-01-01
    • 2011-12-20
    • 1970-01-01
    • 1970-01-01
    • 2020-03-23
    • 2023-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多