Setting background to cover whole screen when using aspect Fit in Swift
当在 swift 中使用 aspect fit 时,屏幕的两侧会被移除。有没有办法通过使用背景颜色填充整个屏幕或使用背景图像来掩盖这一点?
这是在 GameViewController
中创建场景的代码
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
if let view = self.view as! SKView? {
if let scene = SKScene(fileNamed:”mainMenu”) { // setting scene here to aspect fit scene.scaleMode = .aspectFit // Present the scene view.ignoresSiblingOrder = true view.showsFPS = true |
场景适合纵横比且背景设置为清除
具有方面填充的场景
场景适合纵横比且背景设置为绿色
确保为场景设置屏幕尺寸。
1
2 3 |
let screenWidth = UIScreen.main.bounds.width
let screenHeight = UIScreen.main.bounds.height scene?.size = CGSize(width: screenWidth, height: screenHeight) |
之后,设置scene.scaleMode = .aspectFill
如果这不起作用,您可以尝试通过这样做来设置背景颜色,但我不知道此选项是否会为您提供您正在寻找的东西,因为我没有测试它.
1
|
scene?.backgroundColor = UIColor.white //color you want
|
否则尝试设置scene?.size = image.size
- 感谢您如此迅速的反应!我使用宽高比适合的原因是因为当我在 iPad 上而不是手机上使用时,当我使用宽高比填充屏幕时,屏幕会被拉伸。我现在将尝试附上一张图片来解释这一点。再次干杯!
- @Bushers您是否尝试按照建议设置屏幕尺寸?
- 我尝试设置屏幕尺寸,它的效果与纵横填充相同。
- @Bushers 使用 aspectFill 选项设置屏幕大小。收到后也发图片
- 会在一分钟内回复您,只是有一些技术问题
- 现在,你想要的结果是什么?在我看来,aspectFill 正确地填充了屏幕。
- 如果您查看方面填充的图像,您会看到它相对于方面适合切断了屏幕顶部。期望的结果是我可以设置整个屏幕的背景颜色,同时仍然保持屏幕图像显示为适合方面
- 您是否尝试过场景?.backgroundColor = UIColor.white(或您想要的颜色)?
- 是的,如果你看看我附上的第三张图片。图像显示了当我这样做时会发生什么,因为程序将屏幕理解为与实际屏幕尺寸不同的尺寸 – 如果这有意义吗?
- 对于您的第三种情况,这是因为您正在使用 aspectFit。这是为了确保您的整个图像被正确地描绘到场景中。由于无法查看您的代码,我的最后一个建议是设置 scene?.size = image.size
来源:https://www.codenong.com/51410993/