SwiftUI: Why is the height of proxy.safeAreaInsets.bottom equal to keyboardHeight?

核能气质少年 提交于 2021-02-10 20:13:11

问题


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
    }
}

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?


回答1:


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 !!
}


来源:https://stackoverflow.com/questions/64419496/swiftui-why-is-the-height-of-proxy-safeareainsets-bottom-equal-to-keyboardheigh

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!