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

ToggleとToggleライクなボタン (iOSアプリ)

Last updated at Posted at 2024-09-25

ToggleとToggleライクなボタン (iOSアプリ)

ToggleはAppleのデザインガイドラインTogglesによると、リスト形式のみで使用することが推奨されています。

例えば、

Simulator Screenshot - iPhone 15 Pro - 2024-09-26 at 00.27.16.png

上の画面では、リスト形式でToggleを使用しています。これはAppleのデザインガイドラインに沿った設計となります。

それ以外ではToggleライクなボタンの使用を推奨しています。例えば、iOSで元から搭載されているカレンダーアプリの右上で使われています。

Simulator Screenshot - iPhone 15 Pro - 2024-09-26 at 00.29.53.png
Simulator Screenshot - iPhone 15 Pro - 2024-09-26 at 00.29.51.png

普通のボタンの背景を変えることで、Toggleのように表現しています。余計なスペースをとらずに済むのがメリットです。※シミュレータで動かしています。

UIKitでの実装方法

UIBarButtonItemをトグルライクに


class ViewController: UIViewController {
    ...
    // toggleライクなボタン
    lazy var button: UIBarButtonItem = {
        let action = UIAction(handler: {
            [weak self] _ in
            guard let self else {
                return
            }
            self.toggle = !self.toggle
            self.button.isSelected = self.toggle
        })
        let button = UIBarButtonItem(primaryAction: action)
        button.image = UIImage(systemName: "server.rack")
        button.changesSelectionAsPrimaryAction = true
        button.isSelected = self.toggle
        return button
    }()
    private var toggle = true
    ...
}

SwiftUIでの実装方法


Toggle(isOn: $flag) {
    Image(systemName: "flag.fill")
}
.toggleStyle(.button)

toggleStyle(.button)を指定することでtoggleライクなボタンになります。

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