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?

SwiftUIで、画面下に検索バーとボタンを配置する

0
Posted at

iOS26からLiquid Glassが導入され、iOS全体でデザインの変更が行われた。
その一つに検索バーの変更が含まれ、TabView等と統合される形に変更されている。
検索バーとボタンが並んでいるApple Musicの画像

TabViewであれば実装方法が簡単に見つかるが、ToolBarBottomBarに配置すると少し厄介である。
searchableを単体でtoolbarに当てるとBottomBarの位置に表示されるが、ToolbarItemを配置すると上側に移動してしまう。

検索バーが上に来てしまっている画像

解決方法として、システムコンポーネントの位置を指定できるDefaultToolbarItemを使用する。
前後にToolbarSpacerと配置したいToolbarItemを置くことで共存できるようになる。

検索バーとボタンが並んでいる画像
.toolbar {
    DefaultToolbarItem(kind: .search, placement: .bottomBar)
    ToolbarSpacer(.fixed, placement: .bottomBar)
    ToolbarItem(placement: .bottomBar) {
        Button(action: {
            print("add action")
        }) {
            Label("追加", systemImage: "plus")
        }
    }
}
.searchable(
    text: $search,
    placement: .toolbar,
    prompt: String(localized: "検索")
)
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?