Potato
์•ˆ๋…•ํ•˜์„ธ์š”, ๊ฐ์žก๋‹ˆ๋‹ค?๐Ÿฅ” ^___^ ๐Ÿ˜บ github ๋ฐ”๋กœ๊ฐ€๊ธฐ ๐Ÿ‘‰๐Ÿป

potato's iOS Story/๊ฐœ๋ฐœํ•˜๋ฉด์„œ ๋งŒ๋‚œ ์นœ๊ตฌ๋“ค

[SwiftUI] NavigationView ์ด๋™ํ•˜๋‹ค๊ฐ€ Root View๋กœ ๋Œ์•„์˜ค๊ธฐ (์ฒซํ™”๋ฉด์œผ๋กœ ๋Œ์•„์˜ค๋Š” ๋ฐฉ๋ฒ•)

๊ฐ์ž ๐Ÿฅ” 2022. 5. 18. 16:39
๋ฐ˜์‘ํ˜•

 

์•ž์—์„œ ์ž‘์„ฑํ–ˆ๋˜ ๊ธ€์—์„œ, SecondPage์—์„œ NavigationBarHidden(true)์ฝ”๋“œ๋ฅผ ์—†์• ๊ณ , ๋นŒ๋“œ์‹œ์ผœ๋ณด๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ๋  ๊ฒƒ์ด๋‹ค.

 

[SwiftUI] Navigation bar๊ฐ€ ๊ณ„์† ์Œ“์ด๋Š” ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜์ž

SwiftUI๋ฅผ ํ™œ์šฉํ•ด์„œ ์ฒซ ํ”„๋กœ์ ํŠธ๋ฅผ ์ง„ํ–‰ํ•  ๋•Œ, ์€๊ทผํžˆ ๊ฐ„๋‹จํ•˜์ง€๋งŒ ํ•ด๊ฒฐํ•˜๊ธฐ ์–ด๋ ค์› ๋˜ ์ด์Šˆ๊ฐ€ ๋ฐ”๋กœ ํ•ด๋‹น ์ด์Šˆ์˜€๋‹ค. Navigation bar๊ฐ€ ๊ณ„์† ์Œ“์ธ๋‹ค..? ๋ผ๊ณ  ๊ฒ€์ƒ‰ํ•  ์ˆ˜๋„ ์—†๊ณ , ์ฒ˜์Œ swfit๋ฅผ ์ ‘ํ•˜๋Š” ์‚ฌ๋žŒ์œผ

didu-story.tistory.com

์—ฌ๊ธฐ ๋งˆ์ง€๋ง‰ ํ™”๋ฉด์—์„œ ๋’ค๋กœ๊ฐ€๊ธฐ ๋‘๋ฒˆ์„ ๋ˆ„๋ฅด๋ฉด, MainPage๊ฐ€ ๋“ฑ์žฅํ•˜์ง€๋งŒ,,, ๋งŒ์•ฝ ๋ฐ”๋กœ ํ™”๋ฉด์œผ๋กœ ๊ฐ€๊ณ ์‹ถ๋‹ค๋ฉด? ์•„๋ž˜์™€ ๊ฐ™์ด ๋งˆ์ง€๋ง‰ ํ™”๋ฉด์— NavigationLink๋ฅผ ํ†ตํ•ด ์ฒซํ™”๋ฉด์œผ๋กœ ๊ฐ€๊ฒŒ ๋งŒ๋“ค ๊ฒƒ์ด๋‹ค.

// ThirdPage ์ฝ”๋“œ

import SwiftUI

struct ThirdPage: View {
    var body: some View {
        VStack{
            Text("This is Last Page.")
            NavigationLink(destination: ContentView()) {
                Text("Main์œผ๋กœ ๋Œ์•„๊ฐ€๊ธฐ")
                    .foregroundColor(Color.white)
                    .frame(width: 100, height: 60, alignment: .center)
                    .background(RoundedRectangle(cornerRadius: 10)
                        .fill(Color.purple))
            }
        }
    }
}

struct ThirdPage_Previews: PreviewProvider {
    static var previews: some View {
        ThirdPage()
    }
}

์™„๋ฒฝํ•ด ๋ณด์ธ๋‹ค. ๋งˆ์ง€๋ง‰ํ™”๋ฉด์— ๋ฒ„ํŠผ์„ ๋„ฃ์–ด์„œ destination์œผ๋กœ ContentView๋ฅผ ๊ฑธ๋ฉด ๋˜์ง€ ์•Š์„๊นŒ!? ThirdPage์— ๋Œ€ํ•œ ์ฝ”๋“œ๋ฅผ ์ด๋ ‡๊ฒŒ ์ˆ˜์ •ํ•˜๊ณ  ๋นŒ๋“œํ•ด๋ณด๋ฉด ๊ฒฐ๊ณผ๋ฌผ์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

main์„ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅผ๋•Œ๋งˆ๋‹ค NavigationView๊ฐ€ ํ•œ์ธต์”ฉ ๋” ์Œ“์ด๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ์ดˆ๋ฐ˜์— ์ด ๋ฌธ์ œ๋กœ ๊ณ ๋ฏผ์„ ๋งŽ์ด ํ–ˆ์—ˆ๋˜ ๊ฒƒ ๊ฐ™๋‹ค. ์ด๊ฒƒ์€ ์–ด๋–ป๊ฒŒ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์„๊นŒ?

 

ํ•ด๊ฒฐ๋ฐฉ๋ฒ•์€ @Binding์„ ์ด์šฉํ•ด์„œ ๊ฐ’์„ ๋„˜๊ฒจ์ฃผ๋Š” ๊ฒƒ์ด๋‹ค. ์™„์„ฑ์ฝ”๋“œ๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

1. ์ตœ์ƒ๋‹จ ๋ทฐ์—์„œ (๋Œ์•„์˜ค๊ณ ์‹ถ์€ ๋ทฐ) @State ๋ณ€์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด์ค€๋‹ค.

firstNaviLinkActive ๋ผ๊ณ  state๋ณ€์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด์ฃผ๊ณ , ์ดˆ๊ธฐ๊ฐ’์„ false ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•œ๋‹ค. ์ด ๊ฐ’์„ NavigationLink ์—์„œ ๋‹ค์Œ ๋ทฐ๋กœ ๋„˜๊ฒจ์ค„๊ฒƒ์ด๋‹ค. ์ด๋•Œ, $๋กœ ํ‘œํ˜„ํ•œ ๊ฒƒ์€, ํ•ด๋‹น ๋ณ€์ˆ˜๊ฐ€ binding๋œ ๊ฐ’์ด๋ผ๋Š” ๊ฒƒ์„ ์•Œ๋ ค์ฃผ๋Š” ๊ฒƒ์ด๊ณ , isactive๋Š”, ์ธ์ž๊ฐ’์ด true๋กœ ๋ณ€ํ•˜๋ฉด์„œ ๋‘๋ฒˆ์งธ ๋ทฐ๋กœ ๋„˜์–ด๊ฐ„๋‹ค๋Š” ๋œป์ด๋‹ค.

isactive ๋ฅผ ํ•ด์„œ true๋กœ ๋„˜๊ฒจ์ฃผ๋Š” ์ด์œ ๋Š”, true๋กœ ์ด๋™ํ•˜๋‹ค๊ฐ€, ๋งˆ์ง€๋ง‰ ํ™”๋ฉด์—์„œ "๋ฉ”์ธํŽ˜์ด์ง€๋กœ ์ด๋™ํ•˜๊ธฐ"๋ฅผ ๋ˆŒ๋ €์„ ๋•Œ, ์ด ๊ฐ’์„ false๋กœ ์ „ํ™˜ํ•˜๋ฉด์„œ ์ฒซ ํ™”๋ฉด์œผ๋กœ ๋Œ์•„์˜ค๊ฒŒ๋” ์„ค์ •ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋‹ค.

// ContentView ์ฝ”๋“œ

import SwiftUI

struct ContentView: View {
    @State var firstNaviLinkActive = false
    var body: some View {
        NavigationView {
            VStack {
                Text("This is Main page.")
                    .bold()
                NavigationLink(destination: SecondPage(firstNaviLinkActive: $firstNaviLinkActive), isActive: $firstNaviLinkActive) {
                    Text("Click Here")
                        .foregroundColor(Color.white)
                        .frame(width: 100, height: 60, alignment: .center)
                        .background(RoundedRectangle(cornerRadius: 10)
                            .fill(Color.red))
                }
                .navigationBarHidden(true)
            }
        }
    }
}

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

 

2. ๋‘๋ฒˆ์งธ ๋ทฐ์—์„œ @Binding ๋ณ€์ˆ˜๋กœ firstNaviLinkActive ๊ฐ’์„ ๋ฐ›๋Š”๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋‹ค์‹œ ์ด ๊ฐ’์„ 3๋ฒˆ์งธ ๋ทฐ๋กœ ๋„˜๊ธด๋‹ค.

// SecondPage ์ฝ”๋“œ

import SwiftUI

struct SecondPage: View {
        @Binding var firstNaviLinkActive: Bool
    var body: some View {
        VStack{
            Text("This is Second Page.")
            NavigationLink(destination: ThirdPage(firstNaviLinkActive: $firstNaviLinkActive)) {
                Text("Click Here")
                    .foregroundColor(Color.white)
                    .frame(width: 100, height: 60, alignment: .center)
                    .background(RoundedRectangle(cornerRadius: 10)
                        .fill(Color.green))
            }
        }
    }
}

struct SecondPage_Previews: PreviewProvider {
    static var previews: some View {
        SecondPage(firstNaviLinkActive: .constant(true))
    }
}

 

3. ๋งˆ์ง€๋ง‰ ํŽ˜์ด์ง€์—์„œ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ @Binding๋ณ€์ˆ˜๋กœ firstNaviLinkAcive๊ฐ’์„ ์ „๋‹ฌ๋ฐ›์€ ๋’ค, Main์œผ๋กœ ๋Œ์•„๊ฐ€๊ธฐ ๋ผ๋Š” ๋ฒ„ํŠผ์„ ๋งŒ๋“  ํ›„, ๋ฒ„ํŠผ์ด ํด๋ฆญ๋˜์—ˆ์„๋•Œ firstNaviLinkActive๊ฐ’์„ false๋กœ ์ค˜๋ฒ„๋ฆฐ๋‹ค.

// ThirdView ์ฝ”๋“œ (๋งˆ์ง€๋ง‰ํ™”๋ฉด)

import SwiftUI

struct ThirdPage: View {
    @Binding var firstNaviLinkActive: Bool
    var body: some View {
        VStack{
            Text("This is Last Page.")
            Button(action: {
                firstNaviLinkActive = false
            }, label: {
                Text("Main์œผ๋กœ ๋Œ์•„๊ฐ€๊ธฐ")
                    .foregroundColor(Color.white)
                    .frame(width: 100, height: 60, alignment: .center)
                    .background(RoundedRectangle(cornerRadius: 10)
                        .fill(Color.purple))
            })
        }
    }
}


struct ThirdPage_Previews: PreviewProvider {
    static var previews: some View {
        ThirdPage(firstNaviLinkActive: .constant(true))
    }
}

์ด๋ ‡๊ฒŒ ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•˜๊ณ , ์™„์„ฑ๋ณธ์„ ๋ณด๋ฉด ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

 

iOS ์•ฑ๊ฐœ๋ฐœ์„ ํ•˜๋ฉด์„œ, @Binding๋ณ€์ˆ˜์— ๋Œ€ํ•ด์„œ ์ฒ˜์Œ์œผ๋กœ ์ ‘ํ•ด๋ดค๋˜ ์ˆœ๊ฐ„์ด์—ˆ๋‹ค. ๊ฐ’์ด ๋ณ€ํ•œ๊ฒƒ์„ ์œ ์ง€์‹œ์ผœ์ฃผ๋ฉด์„œ, ๊ฐ’์„ ์ „๋‹ฌ๋ฐ›๊ฒŒ ํ•ด์ฃผ๊ณ , ๊ฐ’์ด false๋กœ ๋ฐ”๋€Œ๋ฉด binding๋œ ๋ชจ๋“  ํŽ˜์ด์ง€์˜ ํ•ด๋‹น ๋ณ€์ˆ˜๊ฐ€ ๋‹ค false๋กœ ๋ฐ”๋€Œ๋Š” ์žฌ๋ฐŒ๋Š” ๊ฐœ๋…์ด์—ˆ๋‹ค. 

์ถ”ํ›„ State๋ณ€์ˆ˜์™€ Binding๋ณ€์ˆ˜์— ๋Œ€ํ•ด์„œ ๋” ์ž์„ธํ•˜๊ฒŒ ํฌ์ŠคํŒ…ํ•˜๊ณ  ๊ณต๋ถ€ํ•ด๋ณผ ์˜ˆ์ •์ด๋‹ค.

๋ฐ˜์‘ํ˜•