[英]Create a custom Navigation Bar in SwiftUI So other SwiftUI views should reuse same Nav Bar
在 iOS SwiftUI 中,我們如何為導航欄制作通用布局。 所以我們可以在所有項目中使用它而無需重新編寫相同的代碼。
我們可以使用 ViewBuilder 為通用代碼創建基本視圖,如下所示:
struct BaseView<Content: View>: View {
let content: Content
init(@ViewBuilder content: () -> Content) {
self.content = content()
}
var body: some View {
// To-do: The most important part will go here
}
}
我們如何在 View Builder 或 Base 視圖中添加導航欄代碼?
實現此目的的一種方法是使用自定義視圖作為覆蓋。
例如,考慮下面的代碼,它使用覆蓋創建自定義導航欄:
struct Home: View {
var body: some View {
ScrollView {
// Your Content
}
.overlay {
ZStack {
Color.clear
.background(.ultraThinMaterial)
.blur(radius: 10)
Text("Navigation Bar")
.font(.largeTitle.weight(.bold))
.frame(maxWidth: .infinity, alignment: .leading)
.padding(.leading, 20)
}
.frame(height: 70)
.frame(maxHeight: .infinity, alignment: .top)
}
}
}
.overlay
中的 ZStack 將創建一個看起來像導航欄的視圖。 然后,您可以將其移動到自己的結構視圖中,向其中添加不同的變量並從疊加層中調用它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.