1
1

More than 1 year has passed since last update.

【SwiftUI】NavigationBarにタイトル画像を設定する

Last updated at Posted at 2022-07-26

方法1

import SwiftUI

struct ContentView: View {
    var body: some View {
        NavigationView {
            List {
                ForEach(0..<100) { index in
                    Text("テキスト: \(index)")
                }
            }
            .listStyle(.grouped)
+           .toolbar {
+               ToolbarItem(placement: .navigation) {
+                   Image("instagram")
+                       .resizable()
+                       .aspectRatio(contentMode: .fit)
+                       .padding(.vertical, 5)
+               }
+           }
        }
    }
}

方法2

import SwiftUI

struct ContentView: View {
    var body: some View {
        NavigationView {
            List {
                ForEach(0..<100) { index in
                    Text("テキスト: \(index)")
                }
            }
            .listStyle(.grouped)
+           .toolbar {
+               Image("instagram")
+                   .resizable()
+                   .aspectRatio(contentMode: .fit)
+                   .padding(.vertical, 5)
+           }
        }
    }
}

方法3

import SwiftUI

struct ContentView: View {
    var body: some View {
        NavigationView {
            List {
                ForEach(0..<100) { index in
                    Text("テキスト: \(index)")
                }
            }
            .listStyle(.grouped)
+           .navigationBarItems(leading: HStack {
+               Image("instagram")
+                   .resizable()
+                   .aspectRatio(contentMode: .fit)
+                   .padding(.vertical, 5)
+           })
        }
    }
}

おわり

これら全て同じ見た目になります。
SwiftUIは同じ見た目で複数の書き方ができるので実装時にはかなり迷います。
simulator_screenshot_9B3BF164-96B3-4ACF-BF71-3DB4CD7BE86E.png

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1