我不确定代码中需要更改什么才能使构建成功。这是代码:
import SwiftUI
enum Tab {
case home,message,calender,profile
}
struct TabBarItem: View {
let selectedImage: String
let unSelectedImage: String
let isSelected: Bool
let action: () -> Void
var body: some View {
Button(action: action) {
VStack {
Image(isSelected ? selectedImage : unSelectedImage )
}
}
.foregroundColor(isSelected ? Theme.primaryColor : .secondary)
.frame(maxWidth: .infinity)
}
}
struct TabView: View {
@Binding var tabIdx: Tab
var body: some View {
HStack {
Spacer()
TabBarItem(selectedImage: "ic_selected_home", unSelectedImage: "ic_unselected_home", isSelected: tabIdx == .home) {
self.tabIdx = .home
}
Spacer()
TabBarItem(selectedImage: "ic_selected_message", unSelectedImage: "ic_unselected_message", isSelected: tabIdx == .message) {
self.tabIdx = .message
}
Spacer()
TabBarItem(selectedImage: "ic_selected_calendar", unSelectedImage: "ic_unselected_calendar", isSelected: tabIdx == .calender) {
self.tabIdx = .calender
}
Spacer()
TabBarItem(selectedImage: "ic_selected_profile", unSelectedImage: "ic_unselected_profile", isSelected: tabIdx == .profile) {
self.tabIdx = .profile
}
Spacer()
}
.padding(.bottom, 30)
.frame(height: 80)
.background(
Rectangle()
.fill(Color.white)
.shadow(
color: Color.gray.opacity(0.2),
radius: 15,
x: 0,
y: 0
)
)
}
}
struct BottomTabBar: View {
@State var tabIdx: Tab = .home
var body: some View {
ZStack {
VStack(spacing: 0) {
switch tabIdx {
case .home:
HomeHeader_Previews() *This is where the error comes up*
case .message:
MessageHistoryView()
case .calender:
ScheduleView()
case .profile:
ProfileView()
}
TabView(tabIdx: self.$tabIdx)
}//:VStack
.ignoresSafeArea(.all)
}
}
}
struct BottomTabBar_Previews: PreviewProvider {
static var previews: some View {
BottomTabBar()
}
}
当 Xcode 在 iOS 14 上时,构建工作正常,但我猜因为它是 iOS 15,所以需要更改一些东西?如果您能提供帮助,将不胜感激。我不知道我还需要更改什么才能使代码正常工作,或者也许我是否应该将其改回 iOS 14,但我不知道如何做。
在你的
BottomTabBar
中,我认为 HomeHeader_Previews()
是 PreviewProvider
。
不要将
PreviewProvider
放入 View 主体中,仅使用其他 View。这就是你错误的根源。
尝试使用视图,例如
HomeHeader
中的
BottomTabBar