2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Qiita全国学生対抗戦Advent Calendar 2023

Day 9

【SwiftUI】メニューにPickerを横並びで配置する(iOS17)

Posted at

はじめに

iOS17からPickerにpaletteというスタイルが追加されました。
これはメニュー内で使用すると効果が出るようです。

以前、ボタンを横並びで配置できる方法を紹介しました。
似ていますが、今回はボタンではなくPickerです。

サンプルアプリ

Simulator Screen Recording - iPhone 15 - 2023-12-09 at 22.37.43.gif

実装

import SwiftUI

enum Reaction: Identifiable, CaseIterable {
    case heart, star, bookmark

    var id: Self { self }
    
    var tint: Color {
        return switch self {
        case .heart: .red
        case .star: .yellow
        case .bookmark: .blue
        }
    }
}

struct ContentView: View {
    @State private var selection: Reaction = .heart

    var body: some View {
        Menu {
            Picker("", selection: $selection) {
                Label("heart", systemImage: "heart")
                    .tag(Reaction.heart)
                    .tint(Reaction.heart.tint)
                Label("star", systemImage: "star")
                    .tag(Reaction.star)
                    .tint(Reaction.star.tint)
                Label("bookmark", systemImage: "bookmark")
                    .tag(Reaction.bookmark)
                    .tint(Reaction.bookmark.tint)
            }
            .pickerStyle(.palette)
            .paletteSelectionEffect(.symbolVariant(.fill))
        } label: {
            Text("リアクション")
        }
    }
}

おわり

結構実用性ありそうですね

公式ドキュメント

2
2
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
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?