Something is not working well

Hey guys! I wanted to place the circles along the screen, I took the screen size with UIScreen.main.bounds, so that the circle did not come out of the edges of the screen, and yet it is still coming out! Would any of you be able to help me on how to make the circle stay within the edges of the screen, please?

I'm using SwiftUI and Swift Playgrounds.


struct ContentView: View {
    let screenWidth = UIScreen.main.bounds.width
    let screenHeight = UIScreen.main.bounds.height
    
    var body: some View {
        Circle()
            .foregroundColor(.purple)
            .frame(width: 30, height: 30)
            .position( 
                x: CGFloat.random(in: 0..<screenWidth),
                y:  CGFloat.random(in: 0..<screenHeight)
            )
    }
}

Do you mean you want the circles to appear fully on-screen, and partly off-screen? Isn't it obvious? If the circle is drawn from its centre point, then you need to restrict the area a circle can be drawn to bounds.height and bounds.width minus circle diameter.

You should also take into account safeArea:

.position( 
          x: CGFloat.random(in: 15..<screenWidth-15),
          y:  CGFloat.random(in: 0..<screenHeight-100)
         )

Ok thanks! And in case of an image, how does it work?

Something is not working well
 
 
Q