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

【SwiftUI】Buttonを『画面の幅を基準にした大きさ』に設定する

Last updated at Posted at 2021-02-24

どういうことか

frame(width:)で幅を設定すると機種ごとに見た目がズレるので、画面の幅を基準に設定する。

実装

frameによって設定するが、backgroundの手前に書かなきゃいけないっぽい。


Button(action: {
    print("押された")
}){
    Text("押してください")
        .fontWeight(.bold)
        .font(.system(size: 20))
        // フォントの色
        .foregroundColor(Color.white)
        // 幅を画面いっぱい、高さも指定(maxWidthを使っている場合、heightだと指定できない)
        .frame(maxWidth: .infinity, minHeight: 48)
        // ボタンの色
        .background(Color.blue)
        // 両端にpaddingをかける
        .padding(.horizontal, 32)
}

以上。

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