这是我正在尝试做的事情:
当用户单击当前视图控制器上的按钮时 - 我想呈现一个 UIView 占据整个屏幕 - 状态栏和所有内容 - 并位于当前视图控制器堆栈的顶部。直到 iOS 13,我都是这样做的:
var mainWindow = UIApplication.shared.keyWindow
mainWindow!.addSubview(self.temp_view2!
leadingConstraint = NSLayoutConstraint(item: temp_view2, attribute: NSLayoutConstraint.Attribute.leading, relatedBy: NSLayoutConstraint.Relation.equal, toItem: mainWindow, attribute: NSLayoutConstraint.Attribute.leading, multiplier: 1, constant: 0)
trailingConstraint = NSLayoutConstraint(item: temp_view2, attribute: NSLayoutConstraint.Attribute.trailing, relatedBy: NSLayoutConstraint.Relation.equal, toItem: mainWindow, attribute: NSLayoutConstraint.Attribute.trailing, multiplier: 1, constant: 0)
topConstraint = NSLayoutConstraint(item: temp_view2, attribute: NSLayoutConstraint.Attribute.top, relatedBy: NSLayoutConstraint.Relation.equal, toItem: mainWindow, attribute: NSLayoutConstraint.Attribute.top, multiplier: 1, constant: 0)
bottomConstraint = NSLayoutConstraint(item: temp_view2, attribute: NSLayoutConstraint.Attribute.bottom, relatedBy: NSLayoutConstraint.Relation.equal, toItem: mainWindow, attribute: NSLayoutConstraint.Attribute.bottom, multiplier: 1, constant: 0)
mainWindow?.addConstraints([topConstraint!, bottomConstraint!, leadingConstraint!, trailingConstraint!])
这种方法似乎在 iOS 13 中不起作用,我猜是因为 UIWindow 和 UIScenes 的工作方式——我还没有完全理解。我只在我的应用程序中使用一个窗口。使用这个 SO post How to resolve: 'keyWindow' was deprecated in iOS 13.0我试图看看以下是否可行:
let keyWindow = UIApplication.shared.windows.filter {$0.isKeyWindow}.first
keyWindow?.addSubview(self.temp_view2!)
leadingConstraint = NSLayoutConstraint(item: temp_view2, attribute: NSLayoutConstraint.Attribute.leading, relatedBy: NSLayoutConstraint.Relation.equal, toItem: keyWindow, attribute: NSLayoutConstraint.Attribute.leading, multiplier: 1, constant: 0)
trailingConstraint = NSLayoutConstraint(item: temp_view2, attribute: NSLayoutConstraint.Attribute.trailing, relatedBy: NSLayoutConstraint.Relation.equal, toItem: keyWindow, attribute: NSLayoutConstraint.Attribute.trailing, multiplier: 1, constant: 0)
topConstraint = NSLayoutConstraint(item: temp_view2, attribute: NSLayoutConstraint.Attribute.top, relatedBy: NSLayoutConstraint.Relation.equal, toItem: keyWindow, attribute: NSLayoutConstraint.Attribute.top, multiplier: 1, constant: 0)
bottomConstraint = NSLayoutConstraint(item: temp_view2, attribute: NSLayoutConstraint.Attribute.bottom, relatedBy: NSLayoutConstraint.Relation.equal, toItem: keyWindow, attribute: NSLayoutConstraint.Attribute.bottom, multiplier: 1, constant: 0)
keyWindow?.addConstraints([topConstraint!, bottomConstraint!, leadingConstraint!, trailingConstraint!])
视图确实被添加了,但它一直在后面,在根视图控制器后面,我添加了一条 BringSubviewToFront 线,但这也无济于事。这是它的外观 - 实际上我添加了 3 个视图,如上所述:
我该如何为 >= iOS 13 执行此操作?