0
1

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.

SwiftUiでSegmentの実装方法

Posted at

現在SwiftUIを勉強中なので、さまざまなUIを実装しています。
今回は、SwiftUIでPickerでSegmentedPickerStyleの実装をしてみました。

Xcode Version 14.2

SegmentView.swift
struct SegmentView: View {
    
    enum SegmentType: CaseIterable {
        case one
        case two
    }
    
    @State var selectedLayout: SegmentType = .one
    
    var body: some View {
        ZStack{
            Picker("Layout", selection: $selectedLayout) {
                ForEach(SegmentType.allCases, id: \.self) {
                    type in
                    switch type {
                    case .one:
                        Text("1つ目")
                    case .two:
                        Text("2つ目")
                    }
                }
            }.pickerStyle(SegmentedPickerStyle())
                .padding()
        }
    }
}

こんな感じになりました。

スクリーンショット 2023-08-13 19.39.03.png

Pickerを使用してpickerStyleを設定することによってSegmentの見た目になりました。

.pickerStyle(SegmentedPickerStyle())
こちらを設定すると画像のような表示になります。

0
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?