alertview_swiftui中的7个AlertView选项

本文介绍了在SwiftUI中使用AlertView的7个不同选项,详细解析了如何在iOS应用中创建和定制各种警告视图。
摘要由CSDN通过智能技术生成

alertview

Sadly, one of the drawbacks that they didn’t fix with SwiftUI 2.0 is the AlertView, which remains somewhat simplistic. Here’s the template code to create a basic one:

遗憾的是,它们无法使用SwiftUI 2.0修复的缺点之一是AlertView ,它仍然有些简化。 这是创建基本代码的模板代码:

A simple alertView in Swift 1.0
Swift 1.0中的简单alertView

That gives us this:

这给了我们这个:

Image for post
Basic SwiftIU Alert
基本的SwiftIU警报

However, most alertViews need to do more than warn you that the app has done something — they need more buttons. Now, even in SwiftUI 1.0 you can add two — a primary and a secondary button, along with actions.

但是,大多数alertView需要警告您该应用程序已完成某件事,还需要做更多的事情-他们需要更多的按钮。 现在,即使在SwiftUI 1.0中,您也可以添加两个按钮-一个主按钮和一个辅助按钮以及操作。

Which looks like this:

看起来像这样:

Image for post
Standard SwiftUI Alert with two buttons
带两个按钮的标准SwiftUI警报

So far, so good. But you know how it is — your client, of course, wants more. And more you cannot do with the standard alertView in SwiftUI. You need a different solution. What are your options? Staying with SwiftUI we can use an actionSheet, like this:

到目前为止,一切都很好。 但是您知道情况如何–您的客户当然想要更多。 而且,您不能使用alertView中的标准alertView进行更多操作。 您需要其他解决方案。 您有什么选择? 与SwiftUI一起,我们可以使用actionSheet ,如下所示:

Which looks like this:

看起来像这样:

Image for post
UISwift ActionSheet
UISwift ActionSheet

How many actions can you add? As I write this, I’m not sure. I had imagined there would be some sensible limit. I tried adding sixteen possibilities [which is daft] and it simply scrolls. So it seems limitless, but it isn’t a UIAlertViewController and you know your client will want more.

您可以添加多少个动作? 在我撰写本文时,我不确定。 我曾想过会有一个合理的限制。 我尝试添加16种可能性(很愚蠢),并且它只是滚动。 因此,它似乎是无限的,但它不是UIAlertViewController ,您知道您的客户端会想要更多。

But before we give up, we have a few more tricks in SwiftUI. We can go with a custom alertView. Here’s the code for one of these with a switch in it. Obviously the sky’s the limit on this, you can add whatever controls you can imagine/build.

但是在放弃之前,SwiftUI还有一些技巧。 我们可以使用自定义的alertView 。 这是其中一个带有开关的代码。 显然,这是天空的极限,您可以添加可以想象/构建的任何控件。

That looks like this:

看起来像这样:

Image for post
SwiftIU custom AlertView
SwiftIU自定义AlertView

Of course, you can also make this view appear on the screen using different transitions. Here’s a version that looks much like the action sheet, only this is a custom view.

当然,您也可以使用不同的过渡使该视图显示在屏幕上。 这是一个看起来类似于操作表的版本,只是一个自定义视图。

But hey, you know it’s also possible to use an UIAlertViewController. Just in case he already sold one and all else fails, all is not lost. Here’s the code to do that in SwiftUI:

但是,您知道也可以使用UIAlertViewController 。 万一他已经卖掉了,而其他所有都失败了,一切都不会丢失。 这是在SwiftUI中执行此操作的代码:

Here’s a reminder of what that looks like:

这是对外观的提醒:

Image for post
UIAlertViewContoller in SwiftUI
SwiftUI中的UIAlertViewContoller

And with that, I bring this article to its conclusion.

这样,我便得出本文的结论。

Keep calm, keep coding!

保持冷静,保持编码!

翻译自: https://medium.com/better-programming/7-alertview-options-in-swiftui-691ccb742d22

alertview

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值