SwiftUI如何向修饰符添加条件?

huangapple go评论63阅读模式
英文:

SwiftUI How can we add conditions to modifiers

问题

我想在 macOS 版本为 11 且为新版本时添加 .controlSize 修饰符,但我遇到了问题。我该如何解决这个问题?

Button(action: {
    viewModel.getRoomId()
}, label: {
    Text("获取 Pin")
        .frame(maxWidth: .infinity)
        .padding(.all)
        .frame(height: 44)
    .contentShape(Rectangle()) // 使整个按钮可点击
})
if #available(macOS 11, *) {
.controlSize(.large)
}
英文:

I would like to add the .controlSize modifier when the macOS version is 11 and new but I am having issues. How can I fix this issue

Button(action: {
    viewModel.getRoomId()
}, label: {
    Text("Get Pin")
        .frame(maxWidth: .infinity)
        .padding(.all)
        .frame(height: 44)
    .contentShape(Rectangle()) // Make full button tappable
})
if #available(macOS 11, *) {
.controlSize(.large)
}

答案1

得分: 1

创建一个包装器,将其作为 `View` 的扩展:

```swift
extension View {
    
    func controlSizeIfAvailable(_ controlSize: ControlSize) -> some View {
        if #available(macOS 11, *) {
            return self.controlSize(controlSize)
        }
        else {
            return self
        }
    }
}

并像这样使用它:

        Button(action: {
           viewModel.getRoomId() 
        }, label: {
            Text("获取 Pin")
                .frame(maxWidth: .infinity)
                .padding(.all)
                .frame(height: 44)
                .contentShape(Rectangle()) // 使整个按钮可点击
        })
        .controlSizeIfAvailable(.large)

<details>
<summary>英文:</summary>

Create a wrapper for that modifier as a `View` extension:

extension View {

func controlSizeIfAvailable(_ controlSize: ControlSize) -&gt; some View {
    if #available(macOS 11, *) {
        return self.controlSize(controlSize)
    }
    else {
        return self
    }
}

}

and use it like this:
    Button(action: {
       viewModel.getRoomId() 
    }, label: {
        Text(&quot;Get Pin&quot;)
            .frame(maxWidth: .infinity)
            .padding(.all)
            .frame(height: 44)
        .contentShape(Rectangle()) // Make full button tappable
    })
    .controlSizeIfAvailable(.large)

</details>



# 答案2
**得分**: -1

`controlSize` 修饰符本身在 `macOS 11` 之前可用,只是 `large` 大小不可用。

您可以定义类似以下的内容:

```swift
@ViewBuilder
func largeControlSizeCompat() -> some View {
    if #available(macOS 11.0, *) {
        self.controlSize(.large)
    } else {
        self.controlSize(.regular)
    }
}

然后使用它:

Button("测试") { /* */ }
  .largeControlSizeCompat()
英文:

The controlSize modifier itself is available before macOS 11, just not the large size.

You can define something like this:

@ViewBuilder
func largeControlSizeCompat() -&gt; some View {
    if #available(macOS 11.0, *) {
        self.controlSize(.large)
    } else {
        self.controlSize(.regular)
    }
}

And use it:

Button(&quot;Test&quot;) { /* */ }
  .largeControlSizeCompat()

huangapple
  • 本文由 发表于 2023年7月14日 07:49:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76683910.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定