SwiftUI直通车系列(3)—— 使用导航
SwiftUI直通车系列三(3)—— 使用导航
关于SwiftUI,我们前两篇博客介绍了独立组件的布局与属性设置相关内容,并且介绍了开发中最常用的列表视图的使用。但是一个完整的应用程序不可能是单界面的,如何使用SwiftUI进行界面间的导航跳转,是我们本博客讨论的重点。前两篇博客地址如下:
在本系列的第二篇博客中,我们能够使用SwiftUI来构建列表视图,通常,列表视图用来展示目录页面,当用户点击列表中的某一项时,需要跳转到详情页。在iOS应用中,页面的跳转常用导航控制器进行管理。在SwiftUI中,创建一个导航也是非常简单的,只需要在组件最外层嵌套NavigationView即可。以我们之前的列表视图Demo为例,改写代码如下:
import SwiftUI
import UIKit
struct ContactModel {
var name:String
var phone:String
}
let modelData = [
ContactModel(name:"王小丫", phone:"15137348888"),
ContactModel(name:"李小二", phone:"15137348989")
]
struct ContentView: View {
var body: some View {
VStack (alignment: .leading, spacing: 10) {
Text("Hello, SwiftUI!啊啊啊")
.foregroundColor(Color.red)
.underline()
.font(Font.system(size: 25))
Spacer()
Text("Hello, SwiftUI!")
.foregroundColor(Color.red)
.underline()
.font(Font.system(size: 25))
}
.padding(EdgeInsets(top: 30, leading: 0, bottom: 30, trailing: 0))
}
}
struct Label:UIViewRepresentable {
func makeUIView(context: Context) -> UILabel {
UILabel(frame: .zero)
}
func updateUIView(_ uiView: UILabel, context: Context) {
uiView.text = "Hello"
}
}
struct ContentImage:View {
var body: some View {
Image("demo")
.clipShape(Circle())
.shadow(radius: 30)
}
}
struct RowContent:View {
var contactModel:ContactModel
var body: some View {
HStack(alignment:.top) {
Image("demo").resizable().frame(width: 70, height: 70)
VStack(alignment:.leading, spacing: 10) {
Text(self.contactModel.name).bold().font(Font.system(size: 25))
Text(self.contactModel.phone).font(Font.system(size: 20))
}
Spacer()
}.padding(EdgeInsets(top: 10, leading: 20, bottom: 10, trailing: 20))
}
}
struct ListContent:View {
var body: some View {
NavigationView {
List(modelData, id: \.name) { model in
RowContent(contactModel: model)
}
.navigationBarTitle("通讯录")
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ListContent()
}
}
你只需要关注ListContent结构体即可,其中为列表视图设置了导航,并且定义了导航栏上的标题,效果如下图:
要实现页面的跳转,需要为要触发跳转的组件添加NavigationLink包裹,NavigationLink可以指向要跳转的详情页面,如下:
struct ListContent:View {
var body: some View {
NavigationView {
List(modelData, id: \.name) { model in
NavigationLink(destination: ContentImage()) {
RowContent(contactModel: model)
}
}
.navigationBarTitle("通讯录")
}
}
}
如果详情页面是动态的,在跳转时,我们也可以将数据传递过去,如下:
struct ContentImage:View {
var name:String
var phone:String
var body: some View {
VStack() {
Image("demo")
.clipShape(Circle())
.shadow(radius: 30)
.offset(x: 0, y: -70)
Text(name).offset(x: 0, y: -50)
Text(phone).offset(x: 0, y: -30)
}
}
}
struct ListContent:View {
var body: some View {
NavigationView {
List(modelData, id: \.name) { model in
NavigationLink(destination: ContentImage(name: model.name, phone: model.phone)) {
RowContent(contactModel: model)
}
}
.navigationBarTitle("通讯录")
}
}
}
效果如下图所示:
默认导航的标题是大文字风格的,若要使用常规风格的,需要配置其displayMode属性,如下:
struct ListContent:View {
var body: some View {
NavigationView {
List(modelData, id: \.name) { model in
NavigationLink(destination: ContentImage(name: model.name, phone: model.phone)) {
RowContent(contactModel: model)
}
}
.navigationBarTitle("通讯录", displayMode: .inline)
}
}
}
效果如下图所示:
专注技术,热爱生活,交流技术,也做朋友。
——珲少 QQ群:805263726
关注公众号
低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
-
上一篇
如何优雅的在react-hook中进行网络请求
本文将介绍如何在使用React Hook进行网络请求及注意事项。 前言 Hook是在React 16.8.0版本中新加入的特性,同时在React-Native的0.59.0版本及以上进行了支持,使用hook可以不用class的方式的方式使用state,及类似的生命周期特性。 本片文章通过简单的网络请求数据的demo,来一起进一步认识react-hook这一特性,增加理解,涉及到的hook有useState, useEffect, useReducer等。 使用useState创建js页面 首先创建一个hook的功能页面demoHooks.js, 功能比较简单使用flatlist展示一个文本列表页面 const demoHooks = () => { // 初始值 const [data, setData] = useState({hits: []}); _renderItem = ({item}) => { console.log('rowData', item); return( <View style={{height: 50, backgroundColor: ...
-
下一篇
Linux提权:一些技巧
往往我们能拿下服务器的web服务,却很快被管理员把内网渗透的种子扼杀在提权的萌芽里面。Linux系统的提权过程不止涉及到了漏洞,也涉及了很多系统配置。以下我总结一些提权方法。 前提 已经拿到低权shell,被入侵的机器上面有nc,python,perl等linux非常常见的工具, 有权限上传文件和下载文件、内核漏洞提权、提到脏牛,运维流下两行眼泪,我们留下两行鼻血。内核漏洞是我们几乎最先想到的提权方法。通杀的内核漏洞是十分少见的,因而我们应该先对系统相关的信息进行收集。 查看发行版 cat /etc/issuecat /etc/*-release 查看内核版本 uname -a 这里我找了台机器测试: #uname -aLinux xxxxx 2.6.32-21-generic-pae #32-Ubuntu SMP Fri Apr 16 09:39:35 UTC 2010 i686 GNU/Linux#cat /etc/*-releaseDISTRIB_ID=UbuntuDISTRIB_RELEASE=10.04DISTRIB_CODENAME=lucidDISTRIB_DESCRIP...
相关文章
文章评论
共有0条评论来说两句吧...




微信收款码
支付宝收款码