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 3 years have passed since last update.

SwiftUIでサクッとButtonのイメージを実装する

Last updated at Posted at 2020-06-13

環境

環境
Xcode11.5
Swift5
SwiftUI

何をしているか

SwiftUIフレームワークを用いた場合に, UIButtonに自前のimageを格納して実装しています。

前準備

  1. UIButtonに格納する自前のイメージを作成します。
    button.png

  2. Assetsに1.で作成した画像を格納します。

Screen Shot 2020-06-13 at 19.25.53.png

コードで実装

ContentView.swift
import SwiftUI

struct ContentView: View {
    var body: some View {
        Button(action: {
            
        }, label: {
            // 自作したイメージのファイル名
            Image("button")
            .renderingMode(Image.TemplateRenderingMode?.init(Image.TemplateRenderingMode.original))
        })
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}


シュミレータ実行

以下、このように自前イメージが格納されたUIButtonが表示されます。
Screen Shot 2020-06-13 at 19.32.39.png

参考文献

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?