SwiftUI-iPad上的视图错误

SwiftUI-iPad上的视图错误,swift,xcode,swiftui,Swift,Xcode,Swiftui,我按照教程创建了一个导航菜单,但我有一个问题。在iPhone上看起来很神奇,但在iPad上我想不出问题所在。 似乎我在菜单按钮下有一个“后退”按钮,如果我按下它,它将显示(关闭)页面 截图会告诉我们更多 屏幕: 代码如下: 主视图 import SwiftUI struct MainView: View { @State var selectedTab = "Home" @State var showMenu = false @Envi

我按照教程创建了一个导航菜单,但我有一个问题。在iPhone上看起来很神奇,但在iPad上我想不出问题所在。 似乎我在菜单按钮下有一个“后退”按钮,如果我按下它,它将显示(关闭)页面

截图会告诉我们更多

屏幕:

代码如下: 主视图

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
    }
    
}
导入快捷界面
结构主视图:视图{
@状态变量selectedTab=“主页”
@状态变量showMenu=false
@环境(\.colorScheme)变量colorScheme
var body:一些观点{
ZStack{
颜色(“myblue”)
.ignoresSafeArea()
//配菜
ScrollView(getRect()。高度<750?。垂直:.init(),显示指示器:false,内容:{
侧菜单(selectedTab:$selectedTab)
})
ZStack{
如果colorScheme=.dark{
颜色:黑色
.不透明度(0.5)
.转弯半径(显示菜单?15:0)
阴影(颜色:彩色。黑色。不透明度(0.07),半径:5,x:-5,y:0)
.偏移量(x:showMenu?-25:0)
.padding(.vertical,30)
颜色:黑色
.不透明度(0.4)
.转弯半径(显示菜单?15:0)
阴影(颜色:彩色。黑色。不透明度(0.07),半径:5,x:-5,y:0)
.偏移量(x:showMenu?-50:0)
.padding(.vertical,60)
}否则{
颜色:白色
.不透明度(0.5)
.转弯半径(显示菜单?15:0)
阴影(颜色:彩色。黑色。不透明度(0.07),半径:5,x:-5,y:0)
.偏移量(x:showMenu?-25:0)
.padding(.vertical,30)
颜色:白色
.不透明度(0.4)
.转弯半径(显示菜单?15:0)
阴影(颜色:彩色。黑色。不透明度(0.07),半径:5,x:-5,y:0)
.偏移量(x:showMenu?-50:0)
.padding(.vertical,60)
}
主页(selectedTab:$selectedTab)
.转弯半径(显示菜单?15:1)
}
//缩放和移动视图
.scaleEffect(显示菜单?0.84:1)
.offset(x:showMenu?getRect().width-120:0)
.ignoresSafeArea()
.覆盖(
//菜单按钮
按钮(操作:{
使用动画(.spring()){
showMenu.toggle()
}},标签:{
VStack(间距:5){
胶囊()
.fill(showMenu?Color.white:Color.primary)
.框架(宽度:30,高度:3)
.rotationEffect(.init(度:显示菜单?-50:0))
.偏移量(x:showMenu?2:0,y:showMenu?9:0)
VStack(间距:5){
胶囊()
.fill(showMenu?Color.white:Color.primary)
.框架(宽度:30,高度:3)
胶囊()
.fill(showMenu?Color.white:Color.primary)
.框架(宽度:30,高度:3)
.偏移量(y:显示菜单?-8:0)
}
.rotationEffect(.init(度:显示菜单?50:0))
}
})
.padding()
,对齐方式:。顶部前导
)
}
}
}
结构MainView\u预览:PreviewProvider{
静态var预览:一些视图{
MainView()
}
}
扩展视图{
func getRect()->CGRect{
返回UIScreen.main.bounds
}
}

导入快捷界面
结构主视图:视图{
@绑定变量selectedTab:String
初始化(selectedTab:绑定){
self.\u selectedTab=selectedTab
UITabBar.appearance().ishiden=true
}
var body:一些观点{
//带选项卡的选项卡视图
选项卡视图(选择:$selectedTab){
//观点
主页()
.tag(“主页”)
历史()
.tag(“历史”)
通知()
.tag(“通知”)
设置()
.tag(“设置”)
帮助()
.tag(“帮助”)
}
}
}
结构主视图预览:PreviewProvider{
静态var预览:一些视图{
ContentView()
}
}
结构主页:视图{
var body:一些观点{
导航视图{
文本(“主页”)
.font(.largeTitle)
.fontWeight(.heavy)
.foregroundColor(.primary)
.navigationTitle(“主页”)
}.navigationBarBackButtonHidden(真)
}
}
结构历史:视图{
var body:一些观点{
导航视图{
文本(“历史”)
.font(.largeTitle)
.fontWeight(.heavy)
.foregroundColor(.primary)
.navigationTitle(“历史”)
}
}
}
结构通知:查看{
var body:一些观点{
导航视图{
文本(“通知”)
.font(.largeTitle)
.fontWeight(.heavy)
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")
            
        }
    }
}