LoginSignup
2
3

More than 3 years have passed since last update.

[SwiftUI]backgroundの指定場所ごとにどのように変わるかを調べてみた

Posted at

SwiftUIでbackgroundの指定で思ったように塗りつぶしてくれなかったりしたので、どこで指定したらどのように塗り潰されるかを調べてみました。

コードとView

struct ContentView: View {

    var body: some View {
        VStack {
            Spacer()
            Text("Hello world!")
                .foregroundColor(Color.white)
                .background(Color.green)
                .frame(maxWidth: .infinity, minHeight: 50)
                .background(Color.blue)
                .padding()
                .background(Color.orange)
            Spacer()
        }.frame(width: 320, height: 100)
    }

}

// Present the view controller in the Live View window
PlaygroundPage.current.liveView = UIHostingController(rootView: ContentView())

スクリーンショット 2020-10-06 11.30.38.png

解説

  • frameの前にbackgroundを指定すると、テキストギリギリの範囲で塗りつぶされました(緑色)
  • frameの後にbackgroundがあり、かつbackgroundの後にpaddingがある場合は、padding分内側で塗りつぶされました(青)
  • paddingの後にbackgroundを指定すると、paddingも無視した領域で塗りつぶされました(オレンジ)

このようにbackgroundの位置に注意することで、想定通りの塗りつぶしができそうです。

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