前言
xcode 13.3
iOS 15.2
一、 push跳轉(zhuǎn)
添加跳轉(zhuǎn)時間款咖,首先你要在 NavigationView 中包含的代碼何暮,只要在vc的body中逢并,把代碼放到 NavigationView 就可以了
1、button 跳轉(zhuǎn)(此處是轉(zhuǎn)化成 Text郭卫、Image)
文字button
Button("登錄/注冊") { //文字
print("登錄/注冊") //點擊事件
}
添加跳轉(zhuǎn)事件砍聊,YLMySetting是我的要跳轉(zhuǎn)的頁面,destination下只要是一個 View就可以跳轉(zhuǎn)贰军,你也可以寫一個Text("玻蝌??词疼?")
此處是把純文字的Button轉(zhuǎn)化為Text再添加的跳轉(zhuǎn)事件
NavigationLink {
YLMySetting() //此處為跳轉(zhuǎn)時間俯树,只要是一個view可以跳轉(zhuǎn)過去
} label: {
Text("登錄/注冊")
}
圖片button
此處是把純圖片的Button轉(zhuǎn)化為Image再添加的跳轉(zhuǎn)事件
Button {
print("setting") //點擊事件
} label: {
Image("mine_set") //圖片
}
添加跳轉(zhuǎn)事件
NavigationLink {
YLMySetting()
} label: {
Image("mine_set")
}
2、List 中row 點擊跳轉(zhuǎn)
這塊蘋果的官方文檔中贰盗、demo中都有詳細(xì)說明
NavigationLink {
YLMySetting()
} label: {
YLMineRow(model: models[index]) //此處為row展示
}
3 push 自定義返回
@Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>
在Button按鈕點擊事件或者tap中添加 presentationMode.wrappedValue.dismiss() 返回上一頁
var body: some View {
Button(action: {
presentationMode.wrappedValue.dismiss()
}, label: {
Text("返回")
})
}
二许饿、presented跳轉(zhuǎn)
1、要跳轉(zhuǎn)的頁面設(shè)置
給定一個為false的監(jiān)控值
@State var settingPagePresented: Bool = false
給Text添加單擊手勢舵盈,設(shè)置監(jiān)控值跳轉(zhuǎn)
Text("設(shè)置")
.fullScreenCover(isPresented: $settingPagePresented, content: {
YLMySetting(settingPagePresented: $settingPagePresented)
}).onTapGesture {
settingPagePresented = true
}
2陋率、跳轉(zhuǎn)到的頁面
添加綁定狀態(tài)
@Binding var settingPagePresented: Bool
添加返回按鈕,點擊返回上一頁
NavigationView {
Text("hello world")
.navigationTitle("設(shè)置")
.navigationBarItems(leading: Button(action: {
settingPagePresented = false
}, label: {
Image("icon_back")
}))
.navigationBarTitleDisplayMode(.inline)
}
三秽晚、跳轉(zhuǎn)到 Safari 瀏覽器
使用 Link 可以跳轉(zhuǎn)到 Safari瀏覽器瓦糟,不過鏈接要添加上http,否則跳轉(zhuǎn)不了
Link(destination: URL(string: "https://www.baidu.com")!) {
Text("百度搜索")
}
總結(jié)
1赴蝇、跳轉(zhuǎn)方法菩浙,必須寫在NavigationView 中
2、無論是button還是 Text 或者Image句伶,添加跳轉(zhuǎn)劲蜻,都要把代碼放到label中
3、button點擊事件中考余,沒能成功添加跳轉(zhuǎn)事件先嬉、此處我都是把button 轉(zhuǎn)化為Text 或者Image,有大佬知道怎么添加秃殉,請在評論區(qū)留言坝初,非常感謝