当出现键盘时,简单的SwiftUI背景图像会保持移动 [英] Simple SwiftUI Background Image keeps moving when keyboard appears

查看:83
本文介绍了当出现键盘时,简单的SwiftUI背景图像会保持移动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这真让我发疯.在iOS14中处理背景图片似乎有新的行为.

This is driving me nuts. There seems to be new behaviour for handling background images in iOS14.

所需的效果是:固定的背景图像会填满整个屏幕,而忽略安全区域,并且在弹出键盘时是完全静态的.取而代之的是,即使键盘从底部(??)升起,键盘也会使图像向右滑动.

Desired effect is: a fixed background image that fills the screen, ignoring the safe areas, and is totally static when the keyboard pops up. Instead the keyboard makes the image slide to the right, even though the keyboard is rising from the bottom (??).

这是SwiftUI错误吗?任何想法/变通办法表示赞赏.

Is this a SwiftUI bug? Any ideas/workarounds appreciated.

产生此代码的代码非常小:

The code to produce this is very small:

import SwiftUI

struct ContentView: View {
    
    @State var name = "Name"
    
    var body: some View {
        GeometryReader { geometry in
            VStack {
                TextField("Placeholder", text: $name)
            }
            .frame(width: geometry.size.width, height: geometry.size.height)
            .background(
                Image("bricks")
                    .resizable()
                    .scaledToFill()
                    .edgesIgnoringSafeArea(.all)
            )
        }
    }
}

推荐答案

确定,如下所述,添加 .frame 调用可以使背景图像按需要显示.

Ok, it turns our that adding the .frame call as follows allows the the background image to display as desired.

(特别注意, .frame 调用省略了 height 参数.在以前的许多尝试中,都解决了该高度问题,从而导致了不同的不良行为)

(Note specifically that the .frame call omits the height argument. In many previous tries to solve this the height was included, leading to the different undesired behaviour)

            .background(
                Image("bricks")
                    .resizable()
                    .scaledToFill()
                    .frame(width: geometry.size.width)
                    .edgesIgnoringSafeArea(.all)
            )

这篇关于当出现键盘时,简单的SwiftUI背景图像会保持移动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆