LoginSignup
0
1

More than 3 years have passed since last update.

【SwiftUI】Buttonにおける .padding() の位置を気をつけようという話

Last updated at Posted at 2021-02-25

どういうことか

.padding() の書く位置によって表示が変わる!

どうなるのか

Button(action: {
    print("押された")
}){
    Text("ボタン")
        .fontWeight(.bold)
        .font(.system(size: 20))
        .foregroundColor(Color.white)
        .background(Color.blue)
        .frame(maxWidth: .infinity, minHeight: 48)
        .padding(.horizontal, 32)
}

これだとこう

fdd2b92d411d864aa635d452befaf958.png

ちょっと見づらいが、想定通り画面端からの padding が効いている。
ところが

Button(action: {
    print("押された")
}){
    Text("ボタン")
        .fontWeight(.bold)
        .font(.system(size: 20))
        .foregroundColor(Color.white)
        .background(Color.blue)
        .padding(.horizontal, 32)
        .frame(maxWidth: .infinity, minHeight: 48)
}

c4cbcd62d957f4de60f361e7a626e2eb.png

frame の後ではなく前に書くと効かなくなってしまう。なんてことだ。
で、 padding だけじゃなくて cornerRadius とかも場所で効いたり効かなかったりが変わるっぽい。気をつけよう。

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