mysql 视图条件_SwiftUI中的条件视图

mysql 视图条件

Let's take a look at this simple example:

让我们看一个简单的例子:

If we would want to hide a subtitle text based on some condition there are few options on how to do this.

如果我们想根据某些条件隐藏字幕文本,则如何执行此操作的选项很少。

The most straightforward is to add a State variable and if condition to replace it with the EmptyView.

最简单的方法是添加一个State变量, if有条件if其替换为EmptyView

It’s simple and easy to read but it becomes a bit messy when there are multiple optional blocks and more complex conditions.

它简单易读,但是当存在多个可选块和更复杂的条件时,它会变得有些混乱。

Let’s see how we can use ViewModifier to improve this piece of code. So let’s create one and wrap our condition in it’s body:

让我们看看如何使用ViewModifier改进这段代码。 因此,让我们创建一个并将我们的条件包装在它的主体中:

Please note that we are wrapping our view into the Group view to avoid this opaque type error:

请注意,我们将视图包装到“ Group 视图中,以避免出现这种不透明的类型错误:

“Function declares an opaque return type, but has no return statements in its body from which to infer an underlying type”

“函数声明了不透明的返回类型,但是在其主体中没有用于从其推断基础类型的返回语句”

Of course, there is a couple of other ways, like wrapping it into AnyView or using ViewBuilder property wrapper. It’s up to you what way to choose.

当然,还有一些其他的方式,像包成 AnyView 或使用 ViewBuilder 属性包装。 由您决定选择哪种方式。

Now we can use our shiny new ViewBuilder to make our view optional:

现在,我们可以使用闪亮的新ViewBuilder将视图设为可选:

It already looks a bit nicer. Isn’t it?But, let’s add an extension to the View so it’s even easier to use and read.

它看起来已经更好了。 不是吗?但是,让我们为View添加一个扩展,以便更易于使用和阅读。

Here is how our final code will look like:

我们的最终代码如下所示:

You can find the final result for the Playground in this Gist on Github.

您可以在Github上的这个Gist中找到操场的最终结果。

I hope what I shared did help you!

希望我分享的内容对您有所帮助!

翻译自: https://medium.com/macoclock/conditional-views-in-swiftui-dc09c808bc30

mysql 视图条件

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值