iT邦幫忙

第 11 屆 iThome 鐵人賽

DAY 30
1
Mobile Development

IOS App開發學習日誌系列 第 30

D30 NavigationView&NavigationLink

以往在UIKit framework的時候我們會運用到navigation controller來讓畫面多出頁首的navigation bar來方便我們切換頁面,在swiftui也有類似的功能,不過少了controller來控制,話不多說就讓我們開始吧

NavigationView

以往我們將navigation controller綁定想要顯示的第一個畫面做為進入點,swiftui只需要在程式碼當中把第一個頁面用NavigationView包起來就可以了
https://ithelp.ithome.com.tw/upload/images/20191016/2012098051y2sYRos7.png
swiftui當中一樣可以設定barTitle

接著宣告第二個struct給第二個頁面做使用,並宣告一個變數SecTitle給navigationBarTitle,這稍後會用來存第一個頁面傳來給定的值
https://ithelp.ithome.com.tw/upload/images/20191016/20120980PXmy3eZGC2.png

NavigationLink

使用者通常透過點選button切換頁面,當透過NavigationView管理多層頁面時,它結合NavigationLink的 button切換頁面,我們只要將元件包在NavigationLink裡,就能做出點選元件切換頁面的功能
https://ithelp.ithome.com.tw/upload/images/20191016/20120980FTzAQL2UGS.png
生成NavigationLink時搭配的init如下,參數destination傳入前往的頁面,label傳入顯示的內容
init(destination: Destination, @ViewBuilder label: () -> Label)

最後畫面會呈現如下圖
https://ithelp.ithome.com.tw/upload/images/20191016/2012098030PVTmOniA.png
https://ithelp.ithome.com.tw/upload/images/20191016/20120980Q63jrxDSxF.png


上一篇
D29 1a2b小遊戲
系列文
IOS App開發學習日誌30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 則留言

0
deh
iT邦研究生 1 級 ‧ 2019-10-22 13:33:48

生成NavigationLink時搭配的init如下,參數destination傳入前往的頁面,label傳入顯示的內容
init(destination: Destination, @ViewBuilder label: () -> Label)

上面那段沒看懂@@
不過無視好像也沒影響,照樣能達到目的

0
阿展展展
iT邦好手 1 級 ‧ 2020-02-04 06:11:19

恭喜完賽!

我要留言

立即登入留言