为什么SwiftUI不仅可以加载视图,还不应该在一个愚蠢的行中完成UIKit中可以做的事情。在这种情况下,按钮应该只加载其告知要加载的内容,VideoList(viewModel:.init())应该没有问题。
import Foundation
import SwiftUI
struct SwiftUIView: View {
@State private var isLinkActive = false
let layout = [
GridItem(.adaptive(minimum: 150))
]
var body: some View {
///Top Bar layout
ZStack {
HStack {
Button(action: {
}, label: {
Image("Home")
.resizable()
.frame(width: CGFloat(30), height: CGFloat(30), alignment: .leading)
}).padding(.leading, CGFloat(20))
.padding(.top, 15)
Spacer()
}
HStack {
Text("app")
.frame(alignment: .center)
.font(.title3)
.foregroundColor(Color("Menu"))
.multilineTextAlignment(.center)
.padding(.top, 15)
}
}
///Setup app FEED LINE
HStack{
Text("app FEED")
.padding(.leading, 20.0)
.padding(.top, 100.0)
.font(.largeTitle)
.foregroundColor(Color("Menu"))
Spacer()
}
///Setup Welcom messages
HStack{
Text("Good morning")
.padding(.bottom, 20)
.padding(.leading, 20.0)
.font(.subheadline)
.foregroundColor(Color("Menu"))
Spacer()
Text("...")
.foregroundColor(/*@START_MENU_TOKEN@*/.blue/*@END_MENU_TOKEN@*/)
.font(.largeTitle)
.padding(.trailing, 20.0)
.padding(.bottom, 20)
}
/// Setup navigation home
ScrollView {
LazyVGrid(columns: layout, spacing: 5) {
Button(action: {
print("Button action")
}) {
Image("app")
}
.padding(.vertical, 2.5)
.padding(.horizontal, 2.5)
Button(action: {
print("Button action")
}) {
Image("app")
}
.padding(.vertical, 2.5)
.padding(.horizontal, 2.5)
Button(action: {
self.isLinkActive = true
}) {
Image("app")
}
.padding(.vertical, 2.5)
.padding(.horizontal, 2.5)
Button(action: {
print("Button action")
}) {
Image("app")
}
Button(action: {
print("Button action")
}) {
Image("app")
}
.padding(.vertical, 2.5)
.padding(.horizontal, 2.5)
Button(action: {
print("Button action")
}) {
Image("app")
}
//Add correct images Direction and Contact us
Button(action: {
print("Button action")
}) {
Image("app")
}
Button(action: {
print("Button action")
}) {
Image("app")
}
.padding(.vertical, 2.5)
.padding(.horizontal, 2.5)
}
}
.padding(.horizontal, 2.5)
.padding(.vertical, 2.5)
.navigationBarTitle("", displayMode: .inline)
.navigationBarHidden(true)
.background(
NavigationLink(destination: VideoList(viewModel: .init()), isActive: $isLinkActive) {
EmptyView()
}
.hidden()
)
}
}
struct VideoList: View {
@Environment(\.presentationMode) private var presentationMode
@ObservedObject private(set) var viewModel: ViewModel
@State private var isRefreshing = false
var body: some View {
NavigationView {
List(viewModel.videos.sorted { $0.id > $1.id}, id: \.id) { video in
NavigationLink(
destination: VideoDetails(viewModel: VideoDetails.ViewModel(video: video))) {
VideoRow(video: video)
}
}
.onPullToRefresh(isRefreshing: $isRefreshing, perform: {
self.viewModel.fetchVideos()
})
.onReceive(viewModel.$videos, perform: { _ in
self.isRefreshing = false
})
.navigationBarTitle(viewModel.navigationBarTitle)
}
.onAppear(perform: viewModel.fetchVideos)
}
}
#if DEBUG
struct VideoList_Previews: PreviewProvider {
static var previews: some View {
NavigationView {
VideoList(viewModel: .init())
}
}
}
#endif
在第109行上的按钮未按要求加载VideoList(viewModel:.init())ViewModel。
答案 0 :(得分:0)
NavigationView {}被遗忘了,但是用户界面目前仍然混乱
并添加了固定的HStack...。希望这可以帮助下一个家伙不必花3天时间