当外部屏幕连接到设备时,此功能将运行(该功能运行时,我可以打印到控制台),但是外部屏幕不显示标签,视图也不是白色。我想念什么?

func displayConnected(notification: Notification) {
    let extScreen = notification.object as! UIScreen
    let extWindow = UIWindow(frame: extScreen.bounds)
    extWindow.screen = extScreen

    let extVC = UIViewController()
    extWindow.rootViewController = extVC

    let extView = UIView(frame: extWindow.frame)

    //customize extView
    extView.backgroundColor = UIColor.white

    extWindow.addSubview(extView)
    extWindow.isHidden = false

    let externalLabel = UILabel()
    externalLabel.textAlignment = NSTextAlignment.center
    externalLabel.font = UIFont(name: "Helvetica", size: 50.0)
    externalLabel.frame = extView.bounds
    externalLabel.text = "Hello"
    externalLabel.textColor = UIColor.red
    extView.addSubview(externalLabel)
}

最佳答案

它需要引用本地数组中的窗口。

因此,将其添加到您的vc类中:

var additionalWindows = [UIWindow]()


这对您的功能:

self.additionalWindows.append(extWindow)

08-16 01:40