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

SVGのサイズを指定する方法

Last updated at Posted at 2024-05-29

はじめに

SwiftUIではタブに使う画像のサイズをプログラム上から変更できません。

TabView {
    Text("Foo")
        .tabItem {
            Text("iOS Osushi")
            Image(.tabOsushi)
                .resizable()
                .frame(width: 31, height: 31) // !!!: 効かない
        }
}

そのためSVGファイルを更新する必要があるのですが、サイズを指定する方法がわからなくて調べたので紹介します。

SVGのサイズを指定する

SVGをNeovimなどのテキストエディタで開き、 widthheight を追加するだけです。

tab_osushi.svg
<?xml version="1.0" encoding="UTF-8"?>
- <svg id="_レイヤー_1" data-name="レイヤー 1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 400 400">
+ <svg id="_レイヤー_1" data-name="レイヤー 1" xmlns="http://www.w3.org/2000/svg" width="31px" height="31px" viewBox="0 0 400 400">
...

これだけでSVGのサイズを指定できます。

TabView {
    Text("Foo")
        .tabItem {
-             Text("iOS Osushi")
-             Image(.tabOsushi)
-                 .resizable()
-                 .frame(width: 31, height: 31) // !!!: 効かない
+             Label("iOS Osushi", image: .tabOsushi)
        }
}

おわりに

SVGのサイズを指定する方法でした。
XMLを直接書き換えるのが少し不安なので、もっといい方法を知っている方がいたら教えてくださると嬉しいです :relaxed:

参考リンク

5
2
1

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