我有一个定制的下拉列表,最上面的项是一个按钮(因为.onTapGesture不能处理标题中的项),我想要删除它的点击效果(文本的不透明度变化).当我点击选中的项目时,我想像简单的文本一样,没有点击的效果. enter image description here

我try 了.Animation(无)和.ButtonStyle(.Plain),但没有成功.

                    Button(action: {
                        isSelecting = !isSelecting
                    }) {
                        HStack {
                            Text(selectedItem?.title ?? "Select Item")
                                .animation(.none)
                            Spacer()
                            if items.count > 1 {
                                Image(systemName: "chevron.down")
                            }
                            
                        }
                        .padding(.horizontal)
                    }
                    .buttonStyle(.plain)
                    .animation(nil)
                    .disabled(items.count < 2)
                    .onTapGesture {
                        isSelecting.toggle()
                    }

推荐答案

您可以创建自定义ButtonStyle.在makeBody,不要勾选configuration.isPressed.始终返回一个accentColor色的标签.

struct NoHighlightButtonStyle: ButtonStyle {
    func makeBody(configuration: Configuration) -> some View {
        // handle button role if necessary
        // if configuration.role == .destructive {
        //     configuration.label.foregroundColor(.red)
        // } else {
            configuration.label.foregroundColor(.accentColor)
        // }
    }
}

Ios相关问答推荐

带外部笔划的圆形进度视图

与iPadOS中带有扣件的模式相似的组件是什么?

如何让3张组合的`SF Symbol`图片围绕一个特定点旋转?

许多文本字段的性能问题

如何解决错误 HE0004:无法在 Visual Studio 2022 中加载框架ContentDeliveryServices

更改订单时如何保存商品的位置?

使用 Foundation 解压缩文件

如何让替换视图淡入旧视图而不是交叉淡入淡出?

叠加视图不在堆栈中维护自身 - SwiftUI

AppCenter 错误:/usr/bin/xcodebuild 失败,返回码:65

搜索在 ios xamarin.forms 中不起作用

如何让我的 iOS 应用程序与 api 连接?

检测 iPhone 应用程序中是否存在摄像头?

iOS:应用程序在安装应用程序时未征求用户许可.每次都获得 kCLAuthorizationStatusNotDetermined - Objective-c & Swift

ARKit vs. ARCore vs. Vuforia vs. D'Fusion Mobile vs. Layar SDK

iOS 10 / Xcode 8 设备上的 NSLog 似乎被截断了?为什么?

如果已安装,则重定向到应用程序,否则重定向到 App Store

UITableView:从空白部分隐藏标题

iPhone:什么是 WWDR 中级证书?

错误 itms-90035 - Xcode