[英]SwiftUI view being rendered in small window instead of full-screen when using Xcode 11
I recently created a new SwiftUI project using the beta for Xcode 12. Then I tried to open this project in the non-beta Xcode 11, and after updating the code to use a SwiftUI 1.0-style AppDelegate, I was able to build and run the app. I recently created a new SwiftUI project using the beta for Xcode 12. Then I tried to open this project in the non-beta Xcode 11, and after updating the code to use a SwiftUI 1.0-style AppDelegate, I was able to build and run应用程序。 The issue is that now that I have moved to Xcode 11, the app renders inside of a small frame instead of taking up the whole screen.问题是,现在我已经迁移到 Xcode 11,应用程序在一个小框架内呈现,而不是占据整个屏幕。
Here is a simplified example:这是一个简化的示例:
Xcode 12 vs. Xcode 11 Xcode 12 与 Xcode 11
My simplified view's code is as follows:我的简化视图代码如下:
struct ContentView: View {
var body: some View {
VStack {
Text("Hello World!")
}
}
}
AppDelegate:应用委托:
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
// Called when a new scene session is being created.
// Use this method to select a configuration to create the new scene with.
return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
}
}
SceneDelegate:场景委托:
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
// Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
// If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
// This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
// Create the SwiftUI view that provides the window contents.
let contentView = ContentView()
// Use a UIHostingController as window root view controller.
if let windowScene = scene as? UIWindowScene {
let window = UIWindow(windowScene: windowScene)
window.rootViewController = UIHostingController(rootView: contentView)
self.window = window
window.makeKeyAndVisible()
}
}
I have tried creating a new Xcode 11 project (which renders properly) and comparing its contents to my own project's, but so far I have not been able to find any difference between its AppDelegate, SceneDelegate, ContentView, build settings, etc.我已经尝试创建一个新的 Xcode 11 项目(正确渲染)并将其内容与我自己的项目进行比较,但到目前为止我还没有找到它的 AppDelegate、SceneDelegate、ContentView、构建设置等之间的任何区别。
Is there an option that should be changed in order to have SwiftUI render full-screen in the Xcode 11 project?为了让 SwiftUI 在 Xcode 11 项目中全屏渲染,是否应该更改一个选项?
This is caused because the Xcode 11 project is missing a Launch Screen.这是因为Xcode 11 项目缺少启动屏幕。
This can be resolved by doing the following:这可以通过执行以下操作来解决:
Right click on your project's name, choose New File… , and select Storyboard .右键单击您的项目名称,选择New File…和 select Storyboard 。
In the storyboard, add a new View Controller.在 storyboard 中,添加一个新的 View Controller。
In your app target's settings, find the "App Icons and Launch Images" section.在您的应用目标设置中,找到“应用图标和启动图像”部分。
Select your launch screen from the dropdown: Select 从下拉菜单中启动屏幕:
Now run your application and the SwiftUI view will fill up the whole screen!现在运行您的应用程序,SwiftUI 视图将填满整个屏幕!
The solution to this is to add a LaunchScreen entry in Info.plist as follows -解决方案是在 Info.plist 中添加一个 LaunchScreen 条目,如下所示 -
<key>UILaunchScreen</key> <dict> <key>UIColorName</key> <string>LaunchBackground</string> </dict>
The LaunchBackground must correspond to a color in Assets.xcassets. LaunchBackground 必须对应于 Assets.xcassets 中的颜色。
@ahimsa das mentioned that @ahimsa das提到
If I type in a random string into the "Launch Screen File" field.如果我在“启动屏幕文件”字段中输入随机字符串。 It works.有用。 Any random gibberish like "osd8nfcosd" and it still works.任何像“osd8nfcosd”这样的随机乱码,它仍然有效。 Bizzare lol.奇怪的大声笑。
which is weird but I think it has something to do with the cache on iOS.这很奇怪,但我认为这与 iOS 上的缓存有关。 By typing a random string into the launchscreen file name (in the app's target settings), xcode searches your project for the storyboard file, but since it cant find any, it defaults to a cached image of the launchscreen (correct me if I'm wrong) which would be the image of the very first launchscreen.storyboard
file that was created通过在启动屏幕文件名中输入一个随机字符串(在应用程序的目标设置中),xcode 会在您的项目中搜索 storyboard 文件,但由于它找不到任何文件,因此它默认为启动屏幕的缓存图像(如果我是,请纠正我错误)这将是第一个启动屏幕的图像。创建的launchscreen.storyboard
文件
(in my case this happened as I was using UIKit and wanted to make an app programmatically so I deleted launchscreen.storyboard
and all references to it, which of course, resulted in a small UIWindowScene
) (在我的情况下,这发生在我使用 UIKit 并想以编程方式制作应用程序时,所以我删除launchscreen.storyboard
和所有对它的引用,这当然导致了一个小的UIWindowScene
)
Use ignoresSafeArea
:使用ignoresSafeArea
:
struct ContentView: View {
var body: some View {
VStack {
Text("Hello World!")
}
.ignoresSafeArea()
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.