首页 > 解决方案 > SwiftUI - iPad 上的视图错误

问题描述

我按照教程创建导航菜单,但我遇到了问题。在 iPhone 上它看起来很棒,但在 iPad 上我无法解决问题。似乎我在菜单按钮下有一个“后退”按钮,如果我按下它,它将显示(关闭)页面。

屏幕截图会告诉更多。

屏幕:https ://imgur.com/a/i1Xv32t

这是代码:主视图

import SwiftUI

struct MainView: View {
    @State var selectedTab  = "Home"
    @State var showMenu = false

    
    @Environment(\.colorScheme) var colorScheme
    var body: some View {
        
        ZStack {
            
            Color("myblue")
                .ignoresSafeArea()
            
            // Side Menu
            ScrollView(getRect().height < 750 ? .vertical : .init(), showsIndicators: false, content: {
                SideMenu(selectedTab: $selectedTab)
            })
            
            ZStack {
                if colorScheme == .dark {
                Color.black
                    .opacity(0.5)
                    .cornerRadius(showMenu ? 15 : 0)
                    .shadow(color: Color.black.opacity(0.07), radius: 5, x: -5, y: 0)
                    .offset(x: showMenu ? -25 : 0)
                    .padding(.vertical, 30)
                
                Color.black
                    .opacity(0.4)
                    .cornerRadius(showMenu ? 15 : 0)
                    .shadow(color: Color.black.opacity(0.07), radius: 5, x: -5, y: 0)
                    .offset(x: showMenu ? -50 : 0)
                    .padding(.vertical, 60)
                } else {
                    Color.white
                        .opacity(0.5)
                        .cornerRadius(showMenu ? 15 : 0)
                        .shadow(color: Color.black.opacity(0.07), radius: 5, x: -5, y: 0)
                        .offset(x: showMenu ? -25 : 0)
                        .padding(.vertical, 30)
                    
                    Color.white
                        .opacity(0.4)
                        .cornerRadius(showMenu ? 15 : 0)
                        .shadow(color: Color.black.opacity(0.07), radius: 5, x: -5, y: 0)
                        .offset(x: showMenu ? -50 : 0)
                        .padding(.vertical, 60)
                }
                
                
                Home(selectedTab: $selectedTab)
                    .cornerRadius(showMenu ? 15 : 1)
            }
            // Scalling and Moving The View
            .scaleEffect(showMenu ? 0.84 : 1)
            .offset(x: showMenu ? getRect().width - 120 : 0)
            .ignoresSafeArea()
            .overlay(
            // Menu Button
            Button(action: {
                withAnimation(.spring()) {
                showMenu.toggle()
                }}, label: {
                    VStack (spacing: 5) {
                        
                        Capsule()
                            .fill(showMenu ? Color.white : Color.primary)
                            .frame(width: 30, height: 3)
                            
                            .rotationEffect(.init(degrees: showMenu ? -50 : 0))
                            .offset(x: showMenu ? 2 : 0, y: showMenu ? 9 : 0)
                        
                        VStack (spacing: 5) {
                            
                            Capsule()
                                .fill(showMenu ? Color.white : Color.primary)
                                .frame(width: 30, height: 3)
                            Capsule()
                                .fill(showMenu ? Color.white : Color.primary)
                                .frame(width: 30, height: 3)
                                
                                .offset(y: showMenu ? -8 : 0)
                            
                        }
                        .rotationEffect(.init(degrees: showMenu ? 50 : 0))
                        
                    }
                })
                .padding()
                ,alignment: .topLeading
                )
                
            
        }
        
    }
}

struct MainView_Previews: PreviewProvider {
    static var previews: some View {
        MainView()
    }
}


extension View {
    
    func getRect()->CGRect {
        
        return UIScreen.main.bounds
    }
    
}

import SwiftUI

struct Home: View {
    @Binding var selectedTab: String
    
    init(selectedTab: Binding<String>) {
        
        self._selectedTab = selectedTab
        UITabBar.appearance().isHidden = true
    }
    
    var body: some View {
        
        
        // Tab View with Tabs
        TabView(selection: $selectedTab) {
            
            // Views
            HomePage()
                .tag("Home")
            
            History()
                .tag("History")
            
            Notifications()
                .tag("Notifications")
            
            Settings()
                .tag("Settings")
            
            Help()
                .tag("Help")
        }
    }
}

struct Home_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}


struct HomePage: View {
    var body: some View {
        NavigationView {
            
            Text("Home")
                .font(.largeTitle)
                .fontWeight(.heavy)
                .foregroundColor(.primary)
                .navigationTitle("Home")
            
        }.navigationBarBackButtonHidden(true)
    }
}


struct History: View {
    var body: some View {
        NavigationView {
            
            Text("History")
                .font(.largeTitle)
                .fontWeight(.heavy)
                .foregroundColor(.primary)
                .navigationTitle("History")
            
        }
    }
}

struct Notifications: View {
    var body: some View {
        NavigationView {
            
            Text("Notifications")
                .font(.largeTitle)
                .fontWeight(.heavy)
                .foregroundColor(.primary)
                .navigationTitle("Notifications")
            
        }
    }
}

struct Settings: View {
    var body: some View {
        NavigationView {
            
            Text("Settings")
                .font(.largeTitle)
                .fontWeight(.heavy)
                .foregroundColor(.primary)
                .navigationTitle("Settings")
            
        }
    }
}

struct Help: View {
    var body: some View {
        NavigationView {
            
            Text("Help")
                .font(.largeTitle)
                .fontWeight(.heavy)
                .foregroundColor(.primary)
                .navigationTitle("Help")
            
        }
    }
}

标签: swiftxcodeswiftui

解决方案


Found de issue! Had to remove NavigationView { } from every struct XYZ: View { }.


推荐阅读