iOS开发 - 如何在SwiftUI中显示模态视图?

2020-03-30  本文已影响0人  原来是泽镜啊

在SwiftUI中显示模态视图

简介

这里教大家如何弹出一个简单的模态视图。分别有两个页面,ContentView和GCPresentedView,以下对应简称为A和B。我们要做的是在A视图中点击按钮跳转到B视图,然后再从B视图点击按钮返回到A视图。

步骤

在A视图中创建按钮和模态视图代码

作为一个开发者,有一个学习的氛围跟一个交流圈子特别重要,这是一个我的iOS交流群:413038000,不管你是大牛还是小白都欢迎入驻 ,分享BAT,阿里面试题、面试经验,讨论技术, 大家一起交流学习成长!

<pre class="prettyprint hljs groovy" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; word-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">struct ContentView: View {
@State var isPresented = false

var body: some View {
    Button(action: {
        self.isPresented = true
    }, label: {
        Text("Present Modally")
    })
    .sheet(isPresented: $isPresented) {
        GCPresentedView()
    }
}

}</pre>

使用 @State 对属性进行修饰,在 SwiftUI 内部会自动转换为一对getter,setter,对这个属性进行赋值时会触发视图更新。

$isPresented 能够将值引用(引用方法是在值前方加一个$符号),当引用的值发生改变时,这个改变会向外传递。

.sheet方法用于弹出一个模态视图,在SwiftUI中的定义为。

<pre class="prettyprint hljs swift" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; word-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">public func sheet<Content>(isPresented: Binding<Bool>, onDismiss: (() -> Void)? = nil, @ViewBuilder content: @escaping () -> Content) -> some View where Content : View</pre>

在B视图中创建按钮和关闭模态视图代码

<pre class="prettyprint hljs less" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; word-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">struct GCPresentedView: View {
@Environment(.presentationMode) var mode

var body: some View {
    Button(action: {
        self.mode.wrappedValue.dismiss()
    }, label: {
        Text("Dismiss")
    })
}

}</pre>

@Environment 获取环境变量 presentationMode ,我们可以通过这个变量调用 wrappedValue.dismiss() 可以关闭模态视图。

直接在 Xcode 运行预览

image

总结

使用 SwiftUI 框架处理界面方便很多,不用太多的定义,我们只需要将界面进行描述出来就可以了。这个教程示例中使用到了 Button 和 Text 控件,也用到了@State, Binding, @Environment 技术点。教程很简单,放上来大家一起学习,教程里的代码已放在了GitHub上面,点击 这里获取代码

声明

博文作者:GarveyCalvin

博文出处:http://www.cnblogs.com/GarveyCalvin/

上一篇下一篇

猜你喜欢

热点阅读