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

SwiftUI中.compact样式DatePicker的交互Bug求助

Compact样式DatePicker与Menu共存时的交互异常问题

当DatePicker设置为.compact样式时,存在以下异常交互行为:

  • 在Menu处于打开状态下点击DatePicker,其弹窗无法弹出
  • 后续即便DatePicker已被选中(可通过强调色变化识别),也无法正常触发弹窗功能

复现代码

struct TestView: View {
    
    @State private var date: Date = Date()
    @State private var option: String = "1"
    
    
    var body: some View {
        HStack {
            DatePicker("Date", selection: $date, displayedComponents: .hourAndMinute)
                .datePickerStyle(.compact)
            
            Menu {
                ForEach(1..<4) { index in
                    Button("\(index)") {
                        option = "\(index)"
                    }
                }
            } label: {
                Text(option)
                /// 避免不同选项文本导致灰色矩形宽度变化(例如"30m"比"2:30h"短)
                    .frame(width: 45)
                    .padding(.horizontal, 10)
                    .padding(.vertical, 8)
                    .background {
                        RoundedRectangle(cornerRadius: 8)
                            .fill(Color.gray.opacity(0.15))
                    }
                    .foregroundStyle(UIColor.label.color)
            }
        }
        .labelsHidden()
    }
}


#Preview {
    TestView()
}

问题演示

DatePicker与Menu交互异常

内容的提问来源于stack exchange,提问作者Andrea Mario Lufino

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.04 10:25:26