在overCurrentContext
人的文档中,没有任何内容说明所呈现的视图将调整大小以适应整个屏幕.它根本没有提到任何关于显示的视图大小的内容:
一种显示样式,其中内容显示在另一个视图控制器的内容之上.
文件还说:
因此,如果呈现的视图控制器没有用不透明的内容填充屏幕,底层内容就会显示出来.
这里就是这种情况--您没有编写在屏幕旋转时调整视图大小的代码,所以在设备旋转后,视图"不会填满屏幕"(更不用说"不透明内容"了).
然而,overFullScreen
的文档确实说它将"覆盖"整个屏幕.
一种视图演示样式,其中显示的视图覆盖屏幕.
注意这里不同的用词--"封面",而不是"结束".
无论如何,如果您希望它覆盖definesPresentationContext
的视图控制器,您可以自己向视图添加自动布局约束.
// here I'm assuming "view" is the view you want to cover
let host = UIHostingController(rootView: ...)
host.modalPresentationStyle = .overCurrentContext
host.sizingOptions = [.intrinsicContentSize]
host.presentationController?.delegate = self
host.view.translatesAutoresizingMaskIntoConstraints = false
present(host, animated: true) { [self] in
NSLayoutConstraint.activate([
host.view.leftAnchor.constraint(equalTo: view.leftAnchor),
host.view.rightAnchor.constraint(equalTo: view.rightAnchor),
host.view.topAnchor.constraint(equalTo: view.topAnchor),
host.view.bottomAnchor.constraint(equalTo: view.bottomAnchor),
])
}
您应该在呈现宿主控制器之后添加约束,这就是为什么我将NSLayoutConstraint.activate
放在present
的完成处理程序中.在此之前,所呈现的视图还没有在视图层次 struct 中.
此外,使用intrinsicContentSize
作为大小调整选项.正如它的文档所说,当您使用AutoLayout来布局宿主视图时,应该使用它.