SwiftUI ButtonStyle-如何检查按钮是否已禁用? [英] SwiftUI ButtonStyle - how to check if button is disabled or enabled?

查看:489
本文介绍了SwiftUI ButtonStyle-如何检查按钮是否已禁用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据我的理解,要在SwiftUI中设置按钮的样式,请扩展ButtonStyle并实现func makeBody(configuration: Self.Configuration) -> some View,在其中开始对configuration.label(引用Button视图的引用)进行修改.

To style a button in SwiftUI, according to my understanding, you extend ButtonStyle and implement func makeBody(configuration: Self.Configuration) -> some View within which you start applying modifications to the configuration.label which is a reference to the Button view.

现在,除了label字段之外,ButtonStyle.Configuration还有一个用于isPressed的布尔字段,但这似乎就是全部.

Now, besides the label field, ButtonStyle.Configuration has a boolean field for isPressed, but that's seems to be all.

如何检查该按钮是启用还是禁用?

How do I check if the button is enabled or disabled?

例如,我想在按钮周围绘制边框,如果按钮启用,我希望边框为蓝色,如果按钮禁用,我希望边框为灰色.

For example, I want to draw a border around the button, and I want the border to be blue if the button is enabled and gray if disabled.

我的第一个猜测是这样的:

My first guess is something like this:

    func makeBody(configuration: Self.Configuration) -> some View {
        configuration.label
            .overlay(
                RoundedRectangle(cornerRadius: 4).stroke(configuration.isEnabled ? Color.blue : Color.gray, lineWidth: 1).padding(8)
            )
    }

但是没有isEnabled字段.

Apple提供的PlainButtonStyle显然可以访问此信息,因为声明它的头文件中的doc注释说:该样式可能会施加视觉效果,以指示按钮的按下,聚焦或启用状态.'

The Apple-supplied PlainButtonStyle obviously has access to this information, as the doc comment in the header file that declares it says 'The style may apply a visual effect to indicate the pressed, focused, or enabled state of the button.'

/// A `Button` style that does not style or decorate its content while idle.
///
/// The style may apply a visual effect to indicate the pressed, focused,
/// or enabled state of the button.
@available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
public struct PlainButtonStyle : PrimitiveButtonStyle {
.....

是否可以访问此信息?

在您建议关闭此问题之前,请先阅读以下内容:

Before you suggest closing this question, please read this:

前段时间有一个类似的问题.该问题未指定任何上下文.

There's a similar question asked a while ago. That question does not specify any context.

在这里,我有一个特定的上下文:创建通用的可重用按钮样式.

Here I have a specific context: creating a generic reusable button style.

那里提供的答案是不够的.我不能指望人们将禁用状态传递给样式构造函数.太多的重复工作.

The answer provided there does not suffice. I can't expect people to pass the disabled state to the style constructor. That's too much duplication of effort.

您能想象人们是否必须以这种方式编写代码吗?

Can you imagine if people have to write code this way:

Button() { .... }.disabled(someExprssion).buttonStyle(MyCustomStyle(disabled: someExpression))

显然这是不可取的.

显然,苹果提供的样式可以访问信息,而无需人们再次将禁用状态传递给样式.

Also clearly the Apple supplied style has access to the information without requiring people to pass the disable state again to the style.

如果您关闭问题,将永远阻止任何人在StackOverflow上对该问题提供有用的答案.

If you close the question, you will forever prevent anyone from providing a useful answer to this question on StackOverflow.

在建议关闭之前,请重新考虑.

Please reconsider before you propose closing.

我猜测,如果您可以获取Button视图的EnvironmentValues.isEnabled,那么它可能是正确的值.

I have a guess that if you can get the EnvironmentValues.isEnabled of the Button view then it might be the right value.

另一个提出问题的方法是:在SwiftUI中是否可以获取您未定义自己的视图的环境?

So another way to ask the question is: is it possible in SwiftUI to get the environment of a view that you didn't define yourself?

推荐答案

感谢这个博客,我找到了答案:

I found the answer thanks to this blog: https://swiftui-lab.com/custom-styling/

您可以通过创建包装器视图并在样式struct中使用它来从环境中获取启用状态:

You can get the enabled state from the environment by creating a wrapper view and using it inside the style struct:

struct MyButtonStyle: ButtonStyle {
    func makeBody(configuration: ButtonStyle.Configuration) -> some View {
        MyButton(configuration: configuration)
    }

    struct MyButton: View {
        let configuration: ButtonStyle.Configuration
        @Environment(\.isEnabled) private var isEnabled: Bool
        var body: some View {
            configuration.label.foregroundColor(isEnabled ? Color.green : Color.red)
        }
    }
}

此示例演示如何获取状态并使用它来更改按钮的外观.如果按钮被禁用,它将按钮文本颜色更改为红色,如果启用则将其更改为绿色.

This example demonstrates how to get the state and use it to change the appearance of the button. It changes the button text color to red if the button is disabled or green if it's enabled.

这篇关于SwiftUI ButtonStyle-如何检查按钮是否已禁用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆