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

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

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

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

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

 

โš ๏ธ ์ด์Šˆ์‚ฌํ•ญ

  • ์•„๋ž˜ ์‚ฌ์ง„์ฒ˜๋Ÿผ, navigationLink ์œผ๋กœ ๋ฒ„ํŠผ์„ ๋งŒ๋“ค์–ด์„œ View๊ฐ„์˜ ์ด๋™์„ ํ•˜๊ฒŒ ๋˜๋ฉด, ๋‘๋ฒˆ์งธ๋Š” ๊ดœ์ฐฎ์ง€๋งŒ, ์„ธ๋ฒˆ์งธ๋ถ€ํ„ฐ navigationbar๊ฐ€ ์Œ“์ด๊ฒŒ ๋œ๋‹ค. 
  • NavigationBarHidden(true) ์„ ํ•˜๋ฉด ๋ ์ค„ ์•Œ์•˜์ง€๋งŒ, ๋˜์ง€ ์•Š๋Š”๋‹ค.

 

1. ContentView - ๊ฐ€์žฅ ์ฒซ ๋ฒˆ์งธ ํŽ˜์ด์ง€ ์ฝ”๋“œ

import SwiftUI

struct ContentView: View {
    
    var body: some View {
        NavigationView {
            VStack {
                Text("This is Main page.")
                    .bold()
                NavigationLink(destination: SecondPage()) {
                    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. SecondPage - ๋‘๋ฒˆ ์งธ ํ™”๋ฉด์˜ ์ฝ”๋“œ

import SwiftUI

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

struct SecondPage_Previews: PreviewProvider {
    static var previews: some View {
        SecondPage()
    }
}

3. ThirdPage - ๋งˆ์ง€๋ง‰ ํ™”๋ฉด์˜ ์ฝ”๋“œ

import SwiftUI

struct ThirdPage: View {
    var body: some View {
        Text("This is Last Page.")
    }
}

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

 

๐Ÿ‘‰ ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ์•ˆ๋˜๋Š” ์ด์œ 

  • NavigationView๋ฅผ NavigationLink ๊ฐ€ ์žˆ๋Š” ๋ชจ๋“  ํŽ˜์ด์ง€์— ๊ฑธ์–ด์ฃผ์—ˆ๋‹ค.
  • NavigationView ๋กœ ๋ฌถ์—ฌ์žˆ์–ด๋งŒ, View ์ „ํ™˜์ด ๊ฐ€๋Šฅํ•œ ์ค„ ์•Œ์•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

NavigationView๊ฐ€ ๊ณ„์† ํ•ด์„œ ์Œ“์ด๊ธฐ ๋•Œ๋ฌธ์— ์œ„์— navigation bar ๋„ ๊ณ„์† ์Œ“์ด๋Š” ๊ฒƒ์ด๋‹ค!

ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•์€, NavigationView๋ฅผ NavigationView๊ฐ€ ์‹œ์ž‘๋˜๋Š” ๋ถ€๋ถ„์—๋งŒ ๊ฑธ์–ด์ฃผ๊ณ , ๊ทธ ์ดํ›„์˜ ํ™”๋ฉด์€ ๊ฑธ์ง€ ์•Š์•„๋„ ๋œ๋‹ค. ๊ทธ๋‹ˆ๊นŒ ์œ„ ์ฝ”๋“œ์—์„œ SecondPage์—์„œ NavigationView ์ฝ”๋“œ๋ฅผ ๋นผ๋ฉด ๋œ๋‹ค. 

SecondPage ์ˆ˜์ • ๋ฒ„์ „

import SwiftUI

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

struct SecondPage_Previews: PreviewProvider {
    static var previews: some View {
        SecondPage()
    }
}

 

์ตœ์ข… ํ™”๋ฉด์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

 

 

์ฒ˜์Œ์— ๊ฐœ๋ฐœ์„ ํ•  ๋•Œ ์ด ๋ฌธ์ œ๋กœ ํ—ค๋งธ๋˜ ๊ฒฝํ—˜์ด ์žˆ๋‹ค. ์ƒ๊ฐ๋ณด๋‹ค ๊ฐ„๋‹จํ•œ ๋ฌธ์  ๋ฐ ๋ญ๋ผ๊ณ  ๊ฒ€์ƒ‰ํ•ด์•ผํ• ์ง€๋ชฐ๋ž์—ˆ๋‹ค. ์•ž์œผ๋ก  ์ž˜ํ•ด์•ผ์ง•~

 

๋ฐ˜์‘ํ˜•