SwiftUI:为什么proxy.safeAreaInsets.bottom 的高度等于keyboardHeight? [英] SwiftUI: Why is the height of proxy.safeAreaInsets.bottom equal to keyboardHeight?

查看:50
本文介绍了SwiftUI:为什么proxy.safeAreaInsets.bottom 的高度等于keyboardHeight?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以 iPhone 11 Pro Max 上的 safeAreaInset.bottom 是 34.0,但是当键盘打开时,它会改变 safeAreaInset.bottom 为键盘的高度(346.0 点).有没有办法在键盘打开时访问 safeAreaInset.bottom 值(34.0)?

So the safeAreaInset.bottom on the iPhone 11 Pro Max is 34.0, but when the keyboard is open, it changes the safeAreaInset.bottom to the height of the keyboard (346.0 points). Is there any way to access the safeAreaInset.bottom value (34.0) when the keyboard is open?

作为参考,我的内容视图中有一个几何阅读器:

For reference, my content view has a geometry reader in it:

var body: some View {
    GeometryReader { proxy in
        //I pass in proxy into views in here and then access the safe area with proxy.safeAreaInsets.bottom
    }
}

我还需要它适用于所有设备,似乎为不同的设备制作一堆 if 语句是一个非常糟糕的解决方案.有人有建议吗?

I also need this to be adaptable across all devices, and it seems like making a bunch of if statements for the different devices is a pretty horrible solution. Anyone have suggestions?

推荐答案

这里是可能的解决方案.使用 Xcode 12/iOS 14 测试

Here is possible solution. Tested with Xcode 12 / iOS 14

var body: some View
{
    GeometryReader
    { gp in
        VStack
        {
            Text("Bottom: \(gp.safeAreaInsets.bottom)")
            Spacer()
            TextField("Value", text: $selection)
            Spacer()
        }
    }.ignoresSafeArea(.keyboard, edges: .bottom)     // << here !!
}

这篇关于SwiftUI:为什么proxy.safeAreaInsets.bottom 的高度等于keyboardHeight?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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