You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SwiftUI中Menu Picker被键盘非确定性顶起问题求助

SwiftUI Menu Picker 配合 .ignoresSafeArea(.keyboard) 的异常问题

在SwiftUI的VStack中使用Menu Picker并应用.ignoresSafeArea(.keyboard)修饰符时,Picker按钮会被弹出的键盘顶起,且该现象并非必然出现,表现不具有确定性。

示例代码1

import SwiftUI

struct ContentView: View {
    @State var text = ""
    @State var selection = ""
    let foo = ["a","b","c"]
    var body: some View {
        VStack{
            TextField("test", text: $text)
            Spacer()
            HStack{
                Text("test")
                Menu(content:{
                    Picker(selection: $selection, label:
                        EmptyView()
                    , content: {
                        ForEach(foo,id:\.self){f in
                            Label(f, systemImage: "house")
                                .tag(f)
                        }
                    })
                    Picker(selection: $selection, label:
                        EmptyView()
                    , content: {
                        Label("test", systemImage: "plus.circle.fill")
                            .tag("test 1")
                    })
                },label:{
                    Text("test")
                })
            }
        }
        .ignoresSafeArea(.keyboard)

    }
}

示例代码2(使用Geometry Reader)

import SwiftUI

struct ContentView: View {
    @State var text = ""
    @State var selection = ""
    let foo = ["a","b","c"]
    @FocusState var focus
    var body: some View{
        GeometryReader{_ in
            ZStack{
                Color(UIColor.systemGray6)
                ScrollView{
                    VStack{
                        TextField("title", text: $text)
                            .focused($focus)
                            .frame(maxWidth: .infinity, maxHeight: .infinity)
                            .border(.orange)
                        Spacer()
                    }
                    .frame(maxWidth: .infinity, maxHeight: .infinity)
                }
                overlayInZStack

            }
            .frame(maxWidth: .infinity, maxHeight: .infinity)
        }
        .ignoresSafeArea(.keyboard, edges: .bottom)
    }
    var overlayInZStack: some View {
        VStack{
            Spacer()
            HStack{
                Button("focus"){
                    focus = true
                }
                Menu(content:{
                    Picker(selection: $selection, label:
                        EmptyView()
                    , content: {
                        ForEach(foo,id:\.self){f in
                            Label(f, systemImage: "house")
                                .tag(f)
                        }
                    })
                    Picker(selection: $selection, label:
                        EmptyView()
                    , content: {
                        Label("test", systemImage: "plus.circle.fill")
                            .tag("test 1")
                    })
                },label:{
                    Image(systemName: "house")
                        .padding(10)
                        .background(Circle().foregroundColor(.red))
                        .border(Color.red)
                })
                .border(Color.orange)
            }
            .border(Color.green)
        }
        .frame(maxWidth: .infinity, maxHeight: .infinity)
        .border(Color.black)
        
    }
}

内容的提问来源于stack exchange,提问作者sheldor

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 09:55:22