見出し画像

SwiftUIでいこう! -その他いろいろ(new!)-3

SF Symbols

SF Symbolsを使った画像の表示の方法を簡単にカスタマイズできるようになりました。

hierarchical ・・・ multiple layers
monochrome ・・・single layer filled with the foreground style
multicolor  ・・・・multiple layers with their inherit styles
palette  ・・・multiple layers, with different styles applied to the layers

4種類が用意されています。

Image(systemName: "exclamationmark.triangle.fill")
                .symbolRenderingMode(.hierarchical)
                .font(.system(size: 100))
            
Image(systemName: "exclamationmark.triangle.fill")
                .symbolRenderingMode(.monochrome)
                .font(.system(size: 100))
                
Image(systemName: "exclamationmark.triangle.fill")
                .symbolRenderingMode(.multicolor)
                .font(.system(size: 100))
                
Image(systemName: "exclamationmark.triangle.fill")
                .symbolRenderingMode(.palette)
                .font(.system(size: 100))

で並べてみると


dismissed with a swipe

必ずボタンで閉じる。スワイプで勝手に閉じるのを防ぐ。


show an alert


今までのものは使えないようになり新しく変更されています。

struct ContentView: View {
    @State private var showingAlert = false
    
    var body: some View {
        Button("Show Alert") {
            showingAlert = true
        }
        .alert("Important message", isPresented: $showingAlert) {
            Button("OK", role: .cancel) { }
        }
    }
}

今まではAlert()使っていたがモディファイアで実装することになった。iOS14以前に対応する場合は"Alert()"を使う。


confirmationDialog()

ActionSheetは非推奨(Deprecated)となりconfirmationDialog()を使うようになった。

alertとの違いは

複数の選択肢を指定

することができます。

VStack {
            Text(selection)
            
            Button("Confirm paint color") {
                showingOptions = true
            }
            .confirmationDialog("Select a color", isPresented: $showingOptions, titleVisibility: .visible) {
                Button("Red") {
                    selection = "Red"
                }
                
                Button("Green") {
                    selection = "Green"
                }
                
                Button("Blue") {
                    selection = "Blue"
                }
            }
        }

iOS14以前はActionSheetを使って実装する必要がある。


foreground styles

HStack {
            Image(systemName: "clock.fill")
            Text("Dreams")
        }
        .font(.system(size: 100))
        .foregroundStyle(
            .linearGradient(
                colors: [.red, .black],
                startPoint: .top,
                endPoint: .bottom
            )
        )

実行すると


この記事が気に入ったらサポートをしてみませんか?