1

I have a button set up in ContentView with the modifiers: padding, background, font and foregroundColor.

The preview screen

But when it runs on the simulator, this is what happens to the button:

The simulator screen

Any idea on how to solve this? The code is as follows:

import SwiftUI

 struct ContentView: View {
    
    @State var leftDiceNo = 1
    @State var rightDiceNo = 1
    
    var body: some View {
        ZStack{
            Image("background")
                .resizable()
                .edgesIgnoringSafeArea(.all)
            VStack{
                Image("diceeLogo")
                
                Spacer()
                
                HStack{
                    DiceView(n: leftDiceNo)
                    DiceView(n: rightDiceNo)
                }
                .padding(.horizontal)
                
                Spacer()
                
                Button("Roll"){
                    leftDiceNo = Int.random(in: 1...6)
                    rightDiceNo = Int.random(in: 1...6)
                }
                .padding(5.0)
                .background(Color.red)
                .font(.system(size: 45))
                .foregroundColor(Color.white)
                
            }
            
        }
    }
}
struct DiceView: View {
    
    let n: Int
    
    var body: some View {
        Image("dice\(n)")
            .resizable()
            .aspectRatio(1, contentMode: .fit)
            .padding()
    }
}

struct ContentView_Previews: PreviewProvider {

        static var previews: some View {

        ContentView()

    }

}

1 Answer 1

2

Not a bug. By default background ignores area now if it contacts with it, so disable it explicitly, like

Button("Roll"){
    leftDiceNo = Int.random(in: 1...6)
    rightDiceNo = Int.random(in: 1...6)
}
.padding(5.0)
.background(Color.red, ignoresSafeAreaEdges: [])
//.background(Rectangle().fill(Color.red)) // backward compatible alternate
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! Any idea why the simulator screen behaved differently than the preview screen?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.