使用自定义UIWindow呈现UIAlertController时出现异常

时间:2017-04-09 12:06:30

标签: ios swift exception uialertcontroller uiwindow

我有一个应用程序,在某一点上,我正在使用以下代码显示一个UIAlertController和一个viewcontroller:

let menu = UIAlertController(title: "Menu", message: nil, preferredStyle: .actionSheet)
let cancel = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
menu.addAction(cancel)
self.present(menu, animated: true, completion: nil)

这个工作正常,只要在AppDelegate中我没有设置UIWindow,但是保持为零。 由于我需要使用自定义UIWindow覆盖默认UIWindow,因此我在AppDelegate.swift中有以下代码:

var window: UIWindow? = ThemedWindow()

ThemedWindow:

import Foundation
class ThemedWindow: UIWindow {

    init() {
        super.init(frame: UIScreen.main.bounds)
        //clear the background color of all table views, so we can see the background
        UITableView.appearance().backgroundColor = UIColor.clear

        self.backgroundColor = .red
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
}

在呈现UIAlertController时,我现在得到以下异常:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UIAlertController is expected to have a visual style during transitioning'

任何想法,我做错了什么?

感谢。

更新

在AppDelegate中,我将窗口的初始化更改为

var window: UIWindow? = UIWindow(frame: UIScreen.main.bounds)

我也遇到了崩溃。所以看来,它与子类无关,而与赋值本身无关

更新2

问题解决了:在初始化过程中定义带有屏幕边界的窗口并不是一个好主意(很可能在这个阶段UIScreen维度还没有准备好。)

要解决此问题,我必须将以下行添加到didFinishLaunchingWithOptions:

window!.frame = UIScreen.main.bounds

现在可行了

0 个答案:

没有答案