xcode Launch Screen 的行为与 Launch Image 完全相同
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33701553/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Launch Screen that behaves exactly like Launch Image
提问by Dan Fabulich
In order to get iPad Pro to use full resolution at launch, we have to use a Launch Screen File.
为了让 iPad Pro 在启动时使用全分辨率,我们必须使用 Launch Screen File。
I've never used a Launch Screen XIB/Storyboard before, because my app is backwards compatible to iOS 7. Normally I use a LaunchImage asset catalogto define a specific static image for each device dimension and launch orientation.
我以前从未使用过 Launch Screen XIB/Storyboard,因为我的应用向后兼容 iOS 7。通常我使用LaunchImage 资产目录为每个设备尺寸和启动方向定义特定的静态图像。
Now I'm trying to define a Launch Screen File that acts like a LaunchImage asset catalog, but I'm struggling to do it. In particular:
现在我正在尝试定义一个启动屏幕文件,它的作用类似于 LaunchImage 资产目录,但我正在努力做到这一点。特别是:
1) I don't see a good way to select a different UIImage depending on the exact size of the device, e.g. show one image to iPhone 4S users and another image to iPhone 5 users.
1) 我没有看到根据设备的确切大小选择不同 UIImage 的好方法,例如向 iPhone 4S 用户显示一个图像,向 iPhone 5 用户显示另一个图像。
2) I don't see a way to select a different UIImage for iPad Portrait and iPad Landscape views. Size classes seem to think that both iPad Portrait and iPad Landscape are "Regular" width and "Regular" height, so any UIImage that would show up on iPad Portrait will also show up in iPad Landscape.
2) 我没有看到为 iPad 纵向和 iPad 横向视图选择不同 UIImage 的方法。尺寸类似乎认为 iPad Portrait 和 iPad Landscape 都是“常规”宽度和“常规”高度,因此任何显示在 iPad Portrait 上的 UIImage 也将显示在 iPad Landscape 中。
回答by Kusal Shrestha
The system loads the launch file before launching the app which creates some constraints on what it can contain (some of which may force you back to static image files):
系统在启动应用程序之前加载启动文件,这对其可以包含的内容产生了一些限制(其中一些可能会迫使您返回静态图像文件):
1.The app is not yet loaded so the view hierarchy does not exist and the system can not call any custom view controller setup code you may have in the app (e.g. viewDidLoad)
1.应用程序尚未加载,因此视图层次结构不存在,系统无法调用您在应用程序中可能拥有的任何自定义视图控制器设置代码(例如 viewDidLoad)
2.You can only use standard UIKit classes so you can use UIView or UIViewController but not a custom subclass. If you try to set a custom class you will get an Illegal Configuration error in Xcode.
2.您只能使用标准的 UIKit 类,因此您可以使用 UIView 或 UIViewController 但不能使用自定义子类。如果您尝试设置自定义类,您将在 Xcode 中收到非法配置错误。
3.The launch file can only use basic UIKit views such as UIImageView and UILabel. You cannot use a UIWebView.
3.launch文件只能使用基本的UIKit视图,比如UIImageView和UILabel。您不能使用 UIWebView。
4.If you are using a storyboard you can specify multiple view controllers but there are again some limitations. For example you can embed view controllers in a navigation or tab bar controller but more complex container classes such as UISplitViewController do not work (at least not yet).
4.如果您使用的是故事板,您可以指定多个视图控制器,但同样存在一些限制。例如,您可以将视图控制器嵌入到导航或标签栏控制器中,但更复杂的容器类(例如 UISplitViewController)不起作用(至少现在还没有)。
5.Localizing the launch file does not currently seem to have any effect. The base localization is always used so you will probably want to avoid text on the launch screen.
5.本地化启动文件目前似乎没有任何效果。始终使用基本本地化,因此您可能希望避免在启动屏幕上显示文本。
6.You cannot specify different launch files for iPad and iPhone. This may be a problem if you have significantly different interfaces for those devices as there is only so much you can do with auto layout and size classes.
6.您不能为 iPad 和 iPhone 指定不同的启动文件。如果这些设备的界面明显不同,这可能是一个问题,因为使用自动布局和大小类只能做很多事情。
Note that if you are deploying to iOS 7 you will still need to include the static launch image files. You can include both a launch image file and static launch images. Devices such as the iPhone 6 running iOS 8 will use the launch image file whilst iOS 7 devices will fallback to the launch images.
请注意,如果您要部署到 iOS 7,您仍然需要包含静态启动图像文件。您可以同时包含启动图像文件和静态启动图像。运行 iOS 8 的 iPhone 6 等设备将使用启动图像文件,而 iOS 7 设备将回退到启动图像。
回答by Vitaliy Gozhenko
You can use Size Classes to specify different images for different screen sizes, but this not help you handle cases of iPhone 4S and iPhone 5 screens
您可以使用 Size Classes 为不同的屏幕尺寸指定不同的图像,但这不能帮助您处理 iPhone 4S 和 iPhone 5 屏幕的情况
回答by olynoise
First create all your images. Then, open up your asset catalog and right click -> App Icons & Launch Images -> New iOS Launch Image. This will create an file to drag all your files into. Do that. Name the asset "Launch."
Create a new launch screen storyboard with command-n. Choose iOS -> User Interface -> Launch Screen. Call the file "Launch Screen."
In your Launch Screen storyboard, select the view controller's view
首先创建所有图像。然后,打开您的资产目录并右键单击 -> App Icons & Launch Images -> New iOS Launch Image。这将创建一个文件以将所有文件拖入其中。去做。将资产命名为“启动”。
使用 command-n 创建一个新的启动屏幕故事板。选择 iOS -> 用户界面 -> 启动屏幕。将文件称为“启动屏幕”。
在启动屏幕故事板中,选择视图控制器的视图
[.
[ .
- Find the UIImageView placeholder from the object library in the bottom corner of the right side ba, and drag it into the launch screen view.
- 从右侧ba下角的对象库中找到UIImageView占位符,将其拖入启动屏幕视图中。
- Now, select the image view in the storyboard, and type in the name of the image from your asset catalog.
- 现在,在故事板中选择图像视图,并从您的资产目录中输入图像的名称。
- Control drag from the UIImageView to it's container view to set up auto layout constraints as follows :
- 控制从 UIImageView 拖动到它的容器视图以设置自动布局约束,如下所示:
Optional... if you want it to look nice in your storyboard, select the view and do option-cmd-= to update the frame of the UIImageView.
Go to your info.plist, and type the name of your storyboard ("Launch Screen") for the key "Launch screen interface file base name"
Clean and run.
可选...如果您希望它在故事板中看起来不错,请选择视图并执行 option-cmd-= 以更新 UIImageView 的框架。
转到您的 info.plist,并为“启动屏幕界面文件基本名称”键键入故事板的名称(“启动屏幕”)
清洁并运行。
Hope that helps and that I didn't forget anything!
希望有所帮助,并且我没有忘记任何事情!
回答by Jochen
A different approach is using spacer views that position the correct image in the visible area and move the other one off screen (see my original answer to a similar question here).
一种不同的方法是使用间隔视图,将正确的图像定位在可见区域并将另一个移出屏幕(请参阅我对类似问题的原始答案here)。
You can't provide different images for different screen sizes (iPhone 4, iPhone X, ...), but if you want different images for iPhone and iPadand different images for portrait and landscapethis solution is for you.
你不能提供针对不同屏幕尺寸不同的图像(的iPhone 4,iPhone X,...),但如果你想为iPhone和iPad不同的图像和不同图像的纵向和横向这种解决方案是给你的。
I created an example project on githubif you want to try it out. It works on iPad and iPhone.
如果您想尝试一下,我在 github 上创建了一个示例项目。它适用于 iPad 和 iPhone。
The important constraints are
重要的约束是
PortraitSpacer.width ≤ 5 × view.width
PortraitSpacer.width ≤ 5 × view.height
LandscapeSpacer.width ≥ 5 × view.width
LandscapeSpacer.width ≥ 5 × view.height
PositionSpacer.width = 5 × view.width
where view.width
and view.height
are the main view's width and height.
其中view.width
和view.height
是主视图的宽度和高度。
The PortraitSpacer
positions the portrait image at 5 × min(view.width, view.height)
,
the LandscapeSpacer
positions the landscape image at 5 × max(view.width, view.height)
,
and the PositionSpacer
has the same width as PortraitSpacer
in portrait mode and the same width as LandscapeSpacer
in landscape mode.
的PortraitSpacer
位置在肖像图像5 × min(view.width, view.height)
,所述LandscapeSpacer
位置处的风景图像的5 × max(view.width, view.height)
,并且PositionSpacer
具有相同的宽度PortraitSpacer
在纵向模式和相同的宽度LandscapeSpacer
在横向模式。
We multiply everything with 5 so the two images do not overlap. This works for all devices where the following is true
我们将所有内容乘以 5,这样两个图像就不会重叠。这适用于满足以下条件的所有设备
5 × min(view.width, view.height) + max(view.width, view.height) ≤ 5 × max(view.width, view.height)
In landscape mode this would mean
在横向模式下,这意味着
5 / 4 ≤ view.width / view.height
which is the case for all current devices: iPad has the lowest aspect ratio with 4:3 which is still greater than 5:4.
这是所有当前设备的情况:iPad 的纵横比最低,为 4:3,但仍大于 5:4。
You can then of course configure images per device (iPhone, iPad) in the asset catalog.
然后,您当然可以在资产目录中为每个设备(iPhone、iPad)配置图像。
回答by Ryan Heitner
This is a solution for iPad that can work,
这是一个可以工作的 iPad 解决方案,
for iPhone you can use size classes and I am not posting that solution here.
对于 iPhone,您可以使用 size classes,我不会在此处发布该解决方案。
The solution is to break up your splash screen image into components and to use constraints to manipulate the image for Portrait vs Landscape.
解决方案是将启动画面图像分解为多个组件,并使用约束来操作纵向与横向的图像。
Here is an example of how I did it.
这是我如何做到的一个例子。
Create a launch storyboard for iPad
为 iPad 创建启动故事板
UILaunchStoryboardName~ipad (UILaunchStoryboardName~iphone )
UILaunchStoryboardName~ipad (UILaunchStoryboardName~iphone)
Take your landscape image in my case 2048x1536
在我的情况下拍摄风景图片 2048x1536
I took the landscape image and cut it into three
我把风景图剪成三份
- Center 1536x1536
- Left 256x1536
- Right 256x1536
- 中心 1536x1536
- 左 256x1536
- 右 256x1536
On the launch storyboard I created 3 image views with the following constraints
在启动故事板上,我创建了 3 个具有以下约束的图像视图
Center Piece:
中心件:
- Pin to Top and bottom,
- center in superview,
- maintain aspect ratio
- 固定在顶部和底部,
- 在超级视图中居中,
- 保持纵横比
Left & Right Piece:
左右部分:
- 0 Trailing/Leading to Center,
- Align Top,
- Equal Height
- 0 尾随/通向中心,
- 对齐顶部,
- 等高
Then for the Mode of the view for the left and right I changed it from aspect fill to bottom right and bottom left (worked best for me)
然后对于左右视图的模式,我将其从纵横填充更改为右下角和左下角(对我来说效果最好)
I know this seems like overkill for something that should be simple. My reason for using the launch storyboard over launch images was to cut down on app size. I needed fewer launch images and I could use jpeg rather than png
我知道这对于应该简单的事情来说似乎有点矫枉过正。我在启动图像上使用启动故事板的原因是为了减少应用程序的大小。我需要更少的启动图像,我可以使用 jpeg 而不是 png