Anonymous
Навигация по элементам бокового меню в SwiftUI
Сообщение
Anonymous » 12 дек 2024, 09:05
Я просто хочу спросить о навигации по пунктам бокового меню. Теперь главное в том, что мое приложение имеет 4 вкладки, предположим, tab1, tab2, tab3, tab4, поэтому на экране tab1 я открываю и закрываю боковое меню, это работает идеально, но когда я нажимаю элементы элемента SideMenu, тогда ничего не происходит. Весь код моего бокового меню
Код: Выделить всё
struct SideMenuView: View {
@Binding var isShowing: Bool
@State private var isOn = false
// @State private var selectedDestination: MenuDestination?
// @Environment(\.dismiss) private var dismiss
let profile = UserProfile(
name: "Mr. Henry Edward",
earningPoints: 556,
profileImage: "person.circle.fill"
)
let menuItems = [
SideMenuItem(title: "Edit your info", icon: "edit-profile", destination: .profile),
SideMenuItem(title: "Order History", icon: "order-history", destination: .orderHistory),
SideMenuItem(title: "Reviews", icon: "ranking", destination: .reviews),
SideMenuItem(title: "Setting", icon: "settings", destination: .settings),
SideMenuItem(title: "Change Password", icon: "cart", destination: .changePassword),
SideMenuItem(title: "Franchise Locations", icon: "maps-location", destination: .franchiseLocations),
SideMenuItem(title: "Terms & Conditions", icon: "terms", destination: .termsConditions)
]
var body: some View {
ZStack {
// Dimmed Background
if isShowing {
Color.black
.opacity(0.5)
.ignoresSafeArea()
.onTapGesture {
withAnimation(.spring()) {
isShowing.toggle()
}
}
}
//MARK: - Menu Content
HStack {
VStack(spacing: 0) {
// Profile Header
HStack{
Image(.back)
.onTapGesture {
isShowing = false
}
Spacer()
Toggle(isOn: $isOn) {
}
}
.padding()
.padding(.top,70)
ZStack {
Rectangle()
.fill(Color(.tabBackground))
.frame(width: 290, height: 112)
.cornerRadius(10)
.padding()
HStack(spacing: 16) {
Image(.person)
.resizable()
.aspectRatio(contentMode: .fill)
.frame(width: 40, height: 40)
.clipShape(Circle())
.overlay(Circle().stroke(Color.white, lineWidth: 2))
VStack {
Text(profile.name)
.font(Font.custom("Poppins-Medium", size: 12))
.foregroundStyle(.colorYellow)
Text("**EARNING POINTS** | \(profile.earningPoints)")
.font(Font.custom("Poppins-Medium", size: 11))
.frame(width: 150, height: 22)
.foregroundColor(.black)
.background(Color.white)
.cornerRadius(5)
}
Image(.qrScan)
.applyTapAnimation()
.onTapGesture {
}
}
.padding(.top, 20)
}
ScrollView {
VStack(spacing: 0) {
ForEach(menuItems) { item in
NavigationLink(destination: destinationView(for: item.destination)) {
ProfileMenuItem(item: item)
}
}
Spacer()
AuthButton(title: "Logout", icon: Image(.logout), backgroundColor: .clear, action: {})
.padding()
.foregroundColor(.tabBackground)
}
.background(Color(UIColor.systemBackground))
}
}
.frame(width: UIScreen.main.bounds.width * 0.8)
.background(Color(UIColor.systemBackground))
.cornerRadius(20, corners: [.topRight,.bottomRight])
.offset(x: isShowing ? 0 : -UIScreen.main.bounds.width)
.animation(.spring(), value: isShowing)
Spacer()
}
.ignoresSafeArea()
}
}
}
#Preview {
SideMenuView(isShowing: .constant(true))
}
struct ProfileMenuItem: View {
let item: SideMenuItem
var body: some View {
Button(action: {}) {
HStack(spacing:16) {
Image(item.icon)
.frame(width: 24, height: 24)
.foregroundStyle(.colorBlack)
Text(item.title)
.foregroundColor(.black)
.font(Font.custom("Poppins-Medium", size: 14))
Spacer()
Image(systemName: "chevron.right")
.foregroundColor(.black)
}
.padding()
.background(Color.card)
.cornerRadius(20)
.padding(.horizontal, 26)
.padding(.vertical, 5)
}
}
}
@ViewBuilder
private func destinationView(for destination: MenuDestination) -> some View {
switch destination {
case .profile:
ProfileView()
case .orderHistory:
OrderHistoryView()
case .reviews:
ReviewsView()
case .settings:
SettingsView()
case .changePassword:
ChangePasswordView()
case .franchiseLocations:
FranchiseLocationsView()
case .termsConditions:
TermsConditionsView()
}
}
Я хочу открыть соответствующее представление элемента при нажатии на элемент. Обратите внимание, что мое приложение также имеет 4 вкладки, а на вкладке 1 боковое меню экрана открывается и закрывается при нажатии кнопки, а затем, как мы управляем Просмотр пунктов бокового меню. но я хочу отображать представления элементов siudeMenu независимо, то есть не на вкладках
Подробнее здесь:
https://stackoverflow.com/questions/792 ... in-swiftui
1733983515
Anonymous
Я просто хочу спросить о навигации по пунктам бокового меню. Теперь главное в том, что мое приложение имеет 4 вкладки, предположим, tab1, tab2, tab3, tab4, поэтому на экране tab1 я открываю и закрываю боковое меню, это работает идеально, но когда я нажимаю элементы элемента SideMenu, тогда ничего не происходит. Весь код моего бокового меню [code]struct SideMenuView: View { @Binding var isShowing: Bool @State private var isOn = false // @State private var selectedDestination: MenuDestination? // @Environment(\.dismiss) private var dismiss let profile = UserProfile( name: "Mr. Henry Edward", earningPoints: 556, profileImage: "person.circle.fill" ) let menuItems = [ SideMenuItem(title: "Edit your info", icon: "edit-profile", destination: .profile), SideMenuItem(title: "Order History", icon: "order-history", destination: .orderHistory), SideMenuItem(title: "Reviews", icon: "ranking", destination: .reviews), SideMenuItem(title: "Setting", icon: "settings", destination: .settings), SideMenuItem(title: "Change Password", icon: "cart", destination: .changePassword), SideMenuItem(title: "Franchise Locations", icon: "maps-location", destination: .franchiseLocations), SideMenuItem(title: "Terms & Conditions", icon: "terms", destination: .termsConditions) ] var body: some View { ZStack { // Dimmed Background if isShowing { Color.black .opacity(0.5) .ignoresSafeArea() .onTapGesture { withAnimation(.spring()) { isShowing.toggle() } } } //MARK: - Menu Content HStack { VStack(spacing: 0) { // Profile Header HStack{ Image(.back) .onTapGesture { isShowing = false } Spacer() Toggle(isOn: $isOn) { } } .padding() .padding(.top,70) ZStack { Rectangle() .fill(Color(.tabBackground)) .frame(width: 290, height: 112) .cornerRadius(10) .padding() HStack(spacing: 16) { Image(.person) .resizable() .aspectRatio(contentMode: .fill) .frame(width: 40, height: 40) .clipShape(Circle()) .overlay(Circle().stroke(Color.white, lineWidth: 2)) VStack { Text(profile.name) .font(Font.custom("Poppins-Medium", size: 12)) .foregroundStyle(.colorYellow) Text("**EARNING POINTS** | \(profile.earningPoints)") .font(Font.custom("Poppins-Medium", size: 11)) .frame(width: 150, height: 22) .foregroundColor(.black) .background(Color.white) .cornerRadius(5) } Image(.qrScan) .applyTapAnimation() .onTapGesture { } } .padding(.top, 20) } ScrollView { VStack(spacing: 0) { ForEach(menuItems) { item in NavigationLink(destination: destinationView(for: item.destination)) { ProfileMenuItem(item: item) } } Spacer() AuthButton(title: "Logout", icon: Image(.logout), backgroundColor: .clear, action: {}) .padding() .foregroundColor(.tabBackground) } .background(Color(UIColor.systemBackground)) } } .frame(width: UIScreen.main.bounds.width * 0.8) .background(Color(UIColor.systemBackground)) .cornerRadius(20, corners: [.topRight,.bottomRight]) .offset(x: isShowing ? 0 : -UIScreen.main.bounds.width) .animation(.spring(), value: isShowing) Spacer() } .ignoresSafeArea() } } } #Preview { SideMenuView(isShowing: .constant(true)) } struct ProfileMenuItem: View { let item: SideMenuItem var body: some View { Button(action: {}) { HStack(spacing:16) { Image(item.icon) .frame(width: 24, height: 24) .foregroundStyle(.colorBlack) Text(item.title) .foregroundColor(.black) .font(Font.custom("Poppins-Medium", size: 14)) Spacer() Image(systemName: "chevron.right") .foregroundColor(.black) } .padding() .background(Color.card) .cornerRadius(20) .padding(.horizontal, 26) .padding(.vertical, 5) } } } @ViewBuilder private func destinationView(for destination: MenuDestination) -> some View { switch destination { case .profile: ProfileView() case .orderHistory: OrderHistoryView() case .reviews: ReviewsView() case .settings: SettingsView() case .changePassword: ChangePasswordView() case .franchiseLocations: FranchiseLocationsView() case .termsConditions: TermsConditionsView() } } [/code] Я хочу открыть соответствующее представление элемента при нажатии на элемент. Обратите внимание, что мое приложение также имеет 4 вкладки, а на вкладке 1 боковое меню экрана открывается и закрывается при нажатии кнопки, а затем, как мы управляем Просмотр пунктов бокового меню. но я хочу отображать представления элементов siudeMenu независимо, то есть не на вкладках Подробнее здесь: [url]https://stackoverflow.com/questions/79273989/sidemenu-items-navigation-in-swiftui[/url]