Skip to content

Instantly share code, notes, and snippets.

@TuenTuenna
Created April 8, 2022 00:58
Show Gist options
  • Save TuenTuenna/8ebbada365ef72b30288ca450abf0fdc to your computer and use it in GitHub Desktop.
Save TuenTuenna/8ebbada365ef72b30288ca450abf0fdc to your computer and use it in GitHub Desktop.
SwiftUi Navigation 버튼으로 이동 처리
//
//  ContentView.swift
//  NavigationLinkTest
//
//  Created by Jeff Jeong on 2022/04/08.
//

import SwiftUI
struct ContentView: View {
   
    
    /// 디테일 화면으로 이동해야하는지 여부
  @State var shouldNavigateToDetail = false
   
  var body: some View {
      
      // 네비게이션 처리를 위한 네비게이션뷰
    NavigationView{
       
        
        VStack{
            
            Text("컨텐트뷰").font(.largeTitle)
            
            // 디테일 화면으로 이동 버튼
            Button(action: {
                // 디테일 화면 닫기
                shouldNavigateToDetail = true
            }, label: {
                Text("디테일 화면으로 이동")
                    .foregroundColor(.black)
                    .padding()
                    .background(RoundedRectangle(cornerRadius: 8).fill(.white))
            })
            
            // 네비게이션 링크는 네비게이션 뷰 안에 있으면 됩니다.
            // 라벨이 없는 링크
          NavigationLink(
            destination: DetailView(),
            isActive: $shouldNavigateToDetail,
            label: {
                // 라벨은 보이지 않음 빈 뷰
              EmptyView()
            })
            .navigationBarTitle("Demo", displayMode: .automatic)
        }
        .frame(maxWidth: .infinity, maxHeight: .infinity)
        .background(Color.green.edgesIgnoringSafeArea(.all))
    
    }.navigationViewStyle(StackNavigationViewStyle())
  }
}

// 디테일 화면 뷰
struct DetailView: View {
   
    // 화면 컨트롤을 위함
    @Environment(\.presentationMode) var presentationMode
    
  @State var isActive = false
   
  var body: some View {
      VStack{
          Text("디테일화면").font(.largeTitle)
          Spacer()
          Button(action: {
              // 디테일 화면 닫기
              presentationMode.wrappedValue.dismiss()
          }, label: {
              Text("디테일 화면 닫기 버튼")
                  .foregroundColor(.black)
                  .padding()
                  .background(RoundedRectangle(cornerRadius: 8).fill(.white))
          })
          Spacer()
      }
      .frame(maxWidth: .infinity, maxHeight: .infinity)
      .background(Color.yellow.edgesIgnoringSafeArea(.all))
  }
}


struct ContentView_Previews: PreviewProvider {
  static var previews: some View {
    ContentView()
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment