はじめに
SwiftUI のコンポーネントまとめです。下記ドキュメントにあるやつをまとめました(ターゲットは iOS14 です)。
ソースはこちら↓↓↓
GitHubソース
Text

Text
UILabel 相当?
| 1 | @frozen struct Text | 
簡易実装。フォント、文字色、下線などいろいろ設定できる。
| 1 | Text("Hello, world!") | 
TextField
UITextField, UITextView 相当?
| 1 | struct TextField<Label> where Label : View | 
簡易実装。
| 1 2 3 4 5 6 7 8 9 | @State private var text: String = "" TextField("PlaceHolder",text: $text) { isEditing in     // 編集開始と終了時呼ばれる     print(isEditing) } onCommit: {     // returnキー押下時に呼ばれる     print("Commit") } | 
キーボード表示時に自動スクロールして、return 押下でキーボード閉じてくれた!
style
以下の style があるようです。

- DefaultTextFieldStyle
- PlainTextFieldStyle
- RoundedBorderTextFieldStyle
- SquareBorderTextFieldStyle
 macOS 用
SecureField
UITextField の secureTextEntry  = true 相当?
| 1 | struct SecureField<Label> where Label : View | 
簡易実装。
| 1 2 3 4 5 | @State private var text: String = "" SecureField("PlaceHolder", text: $text) {     print("Commit") } | 
TextEditor
UITextView 相当?
| 1 | struct TextEditor | 
簡易実装。
| 1 2 3 | @State private var text: String = "" TextEditor(text: $text) | 
Images

Image
UIImage 相当?
| 1 | @frozen struct Image | 
簡易実装。
| 1 | Image(systemName: "trash") | 
AsyncImage
iOS 15 以上なので割愛。
| 1 | struct AsyncImage<Content> where Content : View | 
Buttons

Button
UIButton 相当?
| 1 | struct Button<Label> where Label : View | 
簡易実装。
| 1 2 3 | Button("Button") {   print("Tap") } | 
style
以下のような style があるようです。

- DefaultButtonStyle
- BorderlessButtonStyle
- CardButtonStyle
 tvOS 用
- LinkButtonStyle
 macOS 用
- PlainButtonStyle
EditButton
UIViewController の editButton 相当?
| 1 | struct EditButton | 
簡易実装。押下すると「Edit」と「Done」で切り替わる。たぶん NavigationView と併用するやつ。
| 1 | EditButton() | 
PasteButton
macOS 用なので割愛。
| 1 | struct PasteButton | 
Controls

Link
その名の通りリンク。外部ブラウザで指定 URL に遷移する。
| 1 | struct Link<Label> where Label : View | 
簡易実装。
| 1 | Link("am10ぶろぐ", destination: URL(string: "https://www.am10.blog/")!) | 
Menu
ドロップダウンのようなやつ。
| 1 | struct Menu<Label, Content> where Label : View, Content : View | 
簡易実装。
| 1 2 3 4 5 6 7 8 9 10 | Menu("Piyo") {     Button("Hoge") {         print("hoge")     }     Menu("Foo") {         Button("Fuga") {             print("fuga")         }     } } | 
style
以下のような style があるようです。

- DefaultMenuStyle
- BorderlessButtonMenuStyle
- BorderedButtonMenuStyle
 macOS 用
Value Selectors

Toggle
UISwitch 相当?
| 1 | struct Toggle<Label> where Label : View | 
簡易実装。
| 1 2 3 | @State private var isOn = false Toggle("Toggle", isOn: $isOn) | 
style
以下のような style があるようです。

- DefaultToggleStyle
- ButtonToggleStyle
 iOS 15 以上
- CheckboxToggleStyle
 macOS 用
- SwitchToggleStyle
Slider
UISlider 相当?
| 1 | struct Slider<Label, ValueLabel> where Label : View, ValueLabel : View | 
簡易実装。
| 1 2 3 4 5 6 7 8 9 10 | @State private var value = 50.0 Slider(     value: $value,     in: 0...100,     onEditingChanged: { editing in         // 編集開始と終了時に呼ばれる         print("editing: \(editing), value: \(value)")     } ) | 
Stepper
UIStepper 相当?
| 1 | struct Stepper<Label> where Label : View | 
簡易実装。
| 1 2 3 4 5 6 | @State private var value = 50.0 Stepper("Stepper", value: $value, step: 5) { editing in     // 編集開始と終了時に呼ばれる     print("editing: \(editing), value: \(value)") } | 
Picker
UIPicker 相当?
| 1 | struct Picker<Label, SelectionValue, Content> where Label : View, SelectionValue : Hashable, Content : View | 
簡易実装(最初のラベルはなんだろう?)。
| 1 2 3 4 5 6 7 | @State private var value = 1 Picker("Value", selection: $value) {     Text("Value1").tag(0)     Text("Value2").tag(1)     Text("Value3").tag(2) } | 
style
以下の style があるようです。
|  |  | 
- DefaultPickerStyle
- InlinePickerStyle
 Wheel との違いがわからない。。。
- MenuPickerStyle
 項目5つ以上のときに使う?
- RadioGroupPickerStyle
 macOS 用
- SegmentedPickerStyle
 項目2〜5のときに使う?
- WheelPickerStyle
 Inline との違いがわからない。。。
DatePicker
UIDatePicker 相当?
| 1 | struct DatePicker<Label> where Label : View | 
簡易実装。
| 1 2 3 4 5 6 7 | @State private var date = Date() DatePicker(   "Date",   selection: $date,   displayedComponents: [.date, .hourAndMinute] ) | 
style
以下のような style があるようです。

- DefaultDatePickerStyle
- CompactDatePickerStyle
- FieldDatePickerStyle
 macOS 用
- GraphicalDatePickerStyle
- StepperFieldDatePickerStyle
 macOS 用
- WheelDatePickerStyle
ColorPicker
ついに登場したカラーピッカー!(AMColorPickerもよかったら使ってください。。。)
| 1 | struct ColorPicker<Label> where Label : View | 
簡易実装。
| 1 2 3 | @State private var color = Color(.systemBlue) ColorPicker("Color", selection: $color, supportsOpacity: true) | 
Value Indicators

Label
文字とアイコンが表示できる。その名の通りラベル?
| 1 | struct Label<Title, Icon> where Title : View, Icon : View | 
簡易実装。
| 1 | Label("Trash", systemImage: "trash") | 
style
以下のような style があるようです。

- DefaultLabelStyle
- IconOnlyLabelStyle
- TitleAndIconLabelStyle
 iOS 14.5 以上
- TitleOnlyLabelStyle
ProgressView
UIProgressView 相当?
| 1 | struct ProgressView<Label, CurrentValueLabel> where Label : View, CurrentValueLabel : View | 
簡易実装。
| 1 | ProgressView(value: 0.5) | 
style
以下のスタイルがあるようです。

- DefaultProgressViewStyle
- LinearProgressViewStyle
- CircularProgressViewStyle
Gauge
watchOS 用なので割愛。
| 1 | struct Gauge<Label, CurrentValueLabel, BoundsLabel, MarkedValueLabels> where Label : View, CurrentValueLabel : View, BoundsLabel : View, MarkedValueLabels : View | 
Localization
LocalizedStringKey
Text, Toggle, Picker などで文字列を設定すると LocalizedStringKey を作成してローカライズファイルを検索してくれるらしい。
| 1 | @frozen struct LocalizedStringKey | 
簡易実装。
Localizable.strings に下記のように記載。
| 1 2 3 4 5 | // en "hoge" = "Hoge"; // ja "hoge" = "ほげ"; | 
| 1 2 | // 英:Hoge, 日:ほげと表示される。 Text("hoge") | 
Infrequently Used Views
EmptyView
View が空であることを示す View。たぶん自分で生成することはあんまりない?
| 1 | @frozen struct EmptyView | 
簡易実装。
| 1 | EmptyView() | 
EquatableView
子 View が頻繁に更新されないように使うやつ?
A view type that compares itself against its previous value and prevents its child updating if its new value is the same as its old value.
| 1 | @frozen struct EquatableView<Content> where Content : Equatable, Content : View | 
AnyView
型を消してくれるやつ?
| 1 | @frozen struct AnyView | 
たぶんこんな感じでいろんな型の View 返したいときとかに使う?
| 1 2 3 4 5 6 | func makeView(isHoge: Bool) -> AnyView {     if isHoge {         return AnyView(HogeView())     }     return AnyView(FugaView()) } | 
TupleView
うーん。これはちょっとどういうときに使うやつなのかわからない。。。
A View created from a swift tuple of View values.
| 1 | @frozen struct TupleView<T> | 
Deprecated Views
Deprecated になったやつ。
- MenuButton
- PullDownButton
- Alert
- ActionSheet
おわりに
List とかないなと思ったら別のページにあるみたいです。
こちらもまた別の機会にまとめたいと思います。
 
  
  
  
  

コメント
[…] まとめーその1 […]
[…] まとめーその1 […]