【问题标题】:SwiftUI full screen transparent buttonSwiftUI 全屏透明按钮
【发布时间】:2021-01-17 17:28:59
【问题描述】:

我尝试添加全屏透明按钮:

Button(action: {
       // my action
}) {
    Rectangle()
     .opacity(0)
}

但如果.opacity() 小于 0.1 按钮操作停止工作。如何实现全屏透明按钮?

在 iOS 14.3(Sim)、iOS 14.2(iPhone X)、Xcode 12.3 上测试

【问题讨论】:

    标签: ios swift swiftui


    【解决方案1】:

    这是可能的解决方案。使用 Xcode 12.1 / iOS 14.1 测试

    struct DemoClearButton: View {
        var body: some View {
            Color.clear
                .contentShape(Rectangle())
                .onTapGesture {
                    print(">> transparent tapped")
                }
        }
    }
    

    注意:可能你想添加.edgesIgnoringSafeArea(.all)

    【讨论】:

    • iOS 14.3(Sim), iOS 14.2(iPhone X), Xcode 12.3 Color.clear 动作不执行,任何其他颜色都完美。
    • 这听起来像是 14.3 的错误
    • @SergeyDi 它在 iOS 14.3 上按预期工作。
    • @SergeyDi 我正在使用 Xcode 12.3 和 iOS 14.3。 Asperi 的解决方案对我来说效果很好。
    猜你喜欢
    • 1970-01-01
    • 2020-04-08
    • 2015-02-05
    • 2014-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-27
    • 1970-01-01
    相关资源
    最近更新 更多