点击外部选择器文本不打开选择器菜单

发布于 2025-02-10 14:09:45 字数 669 浏览 0 评论 0原文

我有一个picker这样的

Picker ("", selection: $itemIndex){
    ForEach (0..<items.count, id: \.self){ index in
        Text(items[index])
    }
}
.frame(maxWidth: .infinity)
.background(.red)

问题是,选择器延伸到屏幕宽度,但是

在我单击时, 挑选器上显示的文本并不冗长,也许大约是Picker长度的1/20文本,选择器菜单会打开,但是当我单击文本外部,仍在选择器可见性中时,菜单不会打开。

在上图中,当我点击红色侧时,菜单会打开,但是当我点击两个绿色的侧面时,没有

人知道为什么会发生这种情况?

注意:选择器不在表单navigation controller

I have a Picker like this

Picker ("", selection: $itemIndex){
    ForEach (0..<items.count, id: \.self){ index in
        Text(items[index])
    }
}
.frame(maxWidth: .infinity)
.background(.red)

The issue is that the picker extends to the screen width but the text displaying on the Picker is not that lengthy, maybe about 1/20 of the Picker length

Now when I click on the text, the Picker menu opens up, but when I click outside the text, still within the picker visibility, the menu don't open.

enter image description here

In the image above, when I tap on the red side, the menu opens up, but when I tap on both green sides it doesn't

Do anyone have an idea why this is happening?

Note: the picker is not in a Form or a NavigationController

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心碎无痕… 2025-02-17 14:09:45

好的...因此,在这个问题,我能够自定义自己的picker样式。以下是实现此目的的代码

struct CustomPickerStyle: ViewModifier {
    
    @Binding var index: Int
    var items: [String]
    var font: SwiftUI.Font
    var padding: CGFloat
    
    func body(content: Content) -> some View {
        Menu {
            content
        } label: {
            HStack {
                if let labelText = items[index] {
                    Text(labelText)
                        .font(font)
                    Spacer()
                    Image(systemName: "triangle.fill")
                        .resizable()
                        .frame(width: 12, height: 8)
                        .rotationEffect(.degrees(180))
                }
            }
            .padding(padding)
            .frame(alignment: .leading)
            .background(Colors.white)
        }
    }
    
}

extension View {
    
    func customPickerStyle(index: Binding<Int>, items: [String], font: SwiftUI.Font, padding: CGFloat) -> some View {
        self.modifier(CustomPickerStyle(index: index, items: items, font: font, padding: padding))
    }
}

,这就是我在UI中使用它的方式

Picker("", selection: $itemIndex){
    ForEach(0..<items.count, id: \.self){ index in
        Text(items[index])
    }
}
.customPickerStyle(index: $itemIndex, items: items, font: .system(size: 17), padding: 10)
.frame(width: UIScreen.main.bounds.width - 50)
.overlay(RoundedRectangle(cornerRadius: 10.0).stroke(.blue, lineWidth: 1.0))

,这是结果

< img src =“ https://i.sstatic.net/7ittv.png” alt =“在此处输入图像说明”>

Okay... So through the help of this question, I was able to customize my own Picker style. Below is the code to achieve this

struct CustomPickerStyle: ViewModifier {
    
    @Binding var index: Int
    var items: [String]
    var font: SwiftUI.Font
    var padding: CGFloat
    
    func body(content: Content) -> some View {
        Menu {
            content
        } label: {
            HStack {
                if let labelText = items[index] {
                    Text(labelText)
                        .font(font)
                    Spacer()
                    Image(systemName: "triangle.fill")
                        .resizable()
                        .frame(width: 12, height: 8)
                        .rotationEffect(.degrees(180))
                }
            }
            .padding(padding)
            .frame(alignment: .leading)
            .background(Colors.white)
        }
    }
    
}

extension View {
    
    func customPickerStyle(index: Binding<Int>, items: [String], font: SwiftUI.Font, padding: CGFloat) -> some View {
        self.modifier(CustomPickerStyle(index: index, items: items, font: font, padding: padding))
    }
}

And I this is how I used it in my UI

Picker("", selection: $itemIndex){
    ForEach(0..<items.count, id: \.self){ index in
        Text(items[index])
    }
}
.customPickerStyle(index: $itemIndex, items: items, font: .system(size: 17), padding: 10)
.frame(width: UIScreen.main.bounds.width - 50)
.overlay(RoundedRectangle(cornerRadius: 10.0).stroke(.blue, lineWidth: 1.0))

And here is the result

enter image description here

知足的幸福 2025-02-17 14:09:45

我假设您想要这个

Picker ("", selection: $itemIndex){
    ForEach (0..<items.count, id: \.self) { index in
        Text(items[index])
           .frame(maxWidth: .infinity)    // << here !!
    }
}
.background(.red)

或这样做,如果您告诉有关菜单

    Menu {

      // content here

    } label: {
        Text("Menu")
            .frame(maxWidth: .infinity)    // << here !!
    }

一般规则是 - 为标签提供空间,而该标签的空间实际上显示而不是包含控件,因为通常由固有内容设置hit Testing。

I assume you wanted this

Picker ("", selection: $itemIndex){
    ForEach (0..<items.count, id: \.self) { index in
        Text(items[index])
           .frame(maxWidth: .infinity)    // << here !!
    }
}
.background(.red)

or this if you told about Menu

    Menu {

      // content here

    } label: {
        Text("Menu")
            .frame(maxWidth: .infinity)    // << here !!
    }

the general rule is - give space for label which is actually shown, instead of containing control, because hit testing is usually set up by intrinsic content.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文