如何解决键盘打开时如何避免应用缩放?
当我单击 SwiftUI 文本字段并打开键盘时,应用会缩小(显示在 video 中)。
我对这种行为有两个疑问:
- 为什么会发生这种情况?
- 如何避免这种情况发生?
这是我的代码:
struct BestillView: View { // This view is put inside a tab view with .ignoresSafeArea
@State var navn = ""
@State var varsling = true
var body: some View {
NavigationView {
ZStack {
Color("BackgroundColor")
.ignoresSafeArea()
vstack {
Image("Liquid") // This is my image overlayed on the background,i SUSPECT this may be the only element that actually gets zoomed out
.resizable()
.aspectRatio(contentMode: .fit)
.ignoresSafeArea()
Spacer()
}
vstack {
ZStack(alignment: .leading) { // This is where the text field i'm having trouble with is
Color("UnselectedColor")
.frame(height: 50)
.cornerRadius(20.0)
if navn.isEmpty { // I have a separate text element as the placeholder text so i can give it a custom color
Text("Navn")
.foregroundColor(Color("AccentColor"))
.padding()
}
TextField("",text: $navn)
.padding()
}
.frame(width: 300)
Spacer()
.frame(height: 20.0)
// I removed the rest of my code,I don't think it should be necessary in this question - it's only a NavigationLink and a Toggle
}
}
}
}
}
解决方法
您的图像上有 .ignoresSafeArea(),但您实际上在包含图像的 VStack 上需要它。 VStack 正在缩小以适应键盘的安全区域,这也会挤压图像。
,视图实际上并没有缩小;图像正在缩小 - 因为随着视图向上移动,适合的高度越来越小。
您可以将代码更新为:
Image("Liquid")
.aspectRatio(contentMode: .fill)
它会保持大小不变 - 因为宽度会保持不变。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。