1

嗨,我正在为 iPad 和 iPhone 做一个应用程序,所以我有一个条件来检测屏幕尺寸 .regular o .compact 然后我在 iPhone 或 .compact 视图上显示 iPad 导航视图或 tabview。导入 SwiftUI

struct NavigationViewForiPhone : View{
    var body: some View{
        TabView{
            Text("Hi im on iphone or landscape on iPhone no plus")
                .tabItem {
                    Image(systemName: "house.fill")
                    Text("Inicio")
            }

            Text("Hi im on iphone or landscape on iphone plus")
                .tabItem {
                    Image(systemName: "house.fill")
                    Text("Second")
            }
        }
          .background(Color.blue)
         .navigationBarTitle("iPhone View")
    }
}

struct NavigationViewForiPad : View{
    var body: some View{
        NavigationView{
            List{
                NavigationLink(destination: Text("Text hi")){
                    HStack{
                        Text("hi im a menu")
                    }
                }
                NavigationLink(destination: Text("Text hi")){
                    HStack{
                        Text("hi im a menu")
                    }
                }
                NavigationLink(destination: Text("Text hi")){
                    HStack{
                        Text("hi im a menu")
                    }
                }
                NavigationLink(destination: Text("Text hi")){
                    HStack{
                        Text("hi im a menu")
                    }
                }

                .navigationBarTitle("iPad View")
            }
            VStack{
                Text("Hi im on ipad")
                Text("Hi im on iPad or iPhone plus")
                Text("Hi im on ipad")
            }
            .navigationViewStyle(DoubleColumnNavigationViewStyle())
        }
        .background(Color.red)

    }
}

struct ContentView: View {

    // MARK: -  Detectar el tamaño de la pantalla
    @Environment(\.horizontalSizeClass) var sizeClass


    var body: some View{
        Group{

                // MARK: -  Mostrar navegacion con pestañas en iPhone
                if self.sizeClass == .compact
                {
                    withAnimation(.easeInOut(duration: 1) ){
                        return NavigationViewForiPhone()
                    }

                }
                else
                {
                    withAnimation(.easeInOut(duration: 1) ){
                        return NavigationViewForiPad()
                    }

                }


        }


    }
}


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

所以问题是,当在 iPhone 上(加上尺寸和最大尺寸)更改为横向或纵向时,视图的变化并不顺畅我尝试了很多东西来为过渡设置动画,但我找不到方法。

有什么想法或建议吗?

4

0 回答 0