简体   繁体   中英

Launch Screen Image Stretches in SwiftUI

I'm hoping I can get some help explaining why this is happening. I have been working on switching my app I've been learning / building from UIKit to SwitfUI since this seems to be the future... plus it's fun to learn.

I've got my plist setup with the background and image 在此处输入图像描述

And the very first time I run it when the simulator opens or I clear and erase the simulator is this: 在此处输入图像描述

However, every time after I run the app, the image stretches. 在此处输入图像描述

When I try it on my phone, it seems to stretch every time. I'm not sure what is happening here. Is it just that SwiftUI needs more work or is there a setting I'm missing?

This seems to be a SwiftUI Bug, which still isn't fixed. The only way to go around this bug is to go back to storyboards.

As described in this blog post , you have to create a launch screen storyboard and link it to your project.

To do so:

  • create a new file (cmd + n) and select launch screen
  • customize your launch screen inside the new storyboard (don't use SVG format for images)
  • go to your project settings -> "General" and select the launch screen file as your “Launch Screen File”, which creates a new entry in your info.plist
  • remove your old SwiftUI launch screen entries in info.plist

Resize your image to @1x, @2x, and @3x versions when @1x is 200x200px.

Then upload it to Assets folder and change info.plist Image Name .

This worked for me.

I have resolved the same issue with changing the dimensions of SVG.

  1. Select the Launch image from the assets and press the "Space" button.
  2. Open with Subline Text.
  3. Change width & Height ( I give the dimensions of iPhone 12 pro in px, initially it was 100%)

<svg width="390px" height="844px" viewBox="0 0 1500 1500"

I'll mention this because it technically worked for me.

Turn off the device. Turn it on again. Reinstall the app. Launch screen is not stretched anymore!

It's a strange behavior and it makes me wonder if it's a bug with iOS and not with swiftui.

I got this answer from a comment on this article .

If you are using.SVG images for the launch screen (works on device). 1.- In assets, select the image (in list) and select the image (in detail). 2.- Shows the inspector (button on the top right). 3.- In the Resizing option, check Preserve Vector Data. 4.- In the Scales option, select Single Scale.

I hope it will be useful, to this recurring question and even today with Xcode 14 it is not really solved.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM