Draw Rects & Ovals

TODO: collapasable, copyable, download, etc

Can you give me some code for how I would draw in a swiftUI app. I want to do the following that should. work for any sized screen...

In the middle of the screen, I want to draw a 100px tall and 48 pixel wide rectangle that is #CCCCCC in color. Behind that I want #3333333 rectangle that is as wide as the screen and 130 pixels tall. Then I want to take up the rest of teh space with #0011AA. I want to draw a couple of randomly places ovals that are colored #FFAA11. Certainly! Below is a SwiftUI implementation of your requirements. This code will draw a 100px tall and 48px wide rectangle in the center of the screen, with a background rectangle that is as wide as the screen and 130px tall. The remaining space is filled with a specific color, and a couple of randomly placed ovals are drawn.

import SwiftUI

struct ContentView: View {
    var body: some View {
        GeometryReader { geometry in
            ZStack {
                // Background color
                Color(red: 0, green: 0.066, blue: 0.666)
                    .edgesIgnoringSafeArea(.all)
                
                // Background rectangle
                Color(red: 0.2, green: 0.2, blue: 0.2)
                    .frame(width: geometry.size.width, height: 130)
                    .position(x: geometry.size.width / 2, y: geometry.size.height / 2)

                // Center rectangle
                Color(red: 0.8, green: 0.8, blue: 0.8)
                    .frame(width: 48, height: 100)
                    .position(x: geometry.size.width / 2, y: geometry.size.height / 2)
                
                // Randomly placed ovals
                ForEach(0..<2) { _ in
                    OvalView()
                        .position(
                            x: CGFloat.random(in: 0...geometry.size.width),
                            y: CGFloat.random(in: 0...geometry.size.height)
                        )
                }
            }
        }
    }
}

struct OvalView: View {
    var body: some View {
        Ellipse()
            .fill(Color(red: 1.0, green: 0.666, blue: 0.066))
            .frame(width: 50, height: 80)
    }
}

@main
struct MyApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

Explanation

  1. Background Color: The Color(red: 0, green: 0.066, blue: 0.666) is used to fill the entire background, representing #0011AA.
  2. Background Rectangle: A Color view is used to create a rectangle that is as wide as the screen and 130 pixels tall, centered vertically and horizontally.
  3. Center Rectangle: Another Color view creates a 48x100 pixels rectangle centered on the screen.
  4. Randomly Placed Ovals: The ForEach loop creates two instances of OvalView, each with a randomly determined position within the screen bounds.

This SwiftUI code will adapt to different screen sizes while maintaining the specified design elements.Above this overal image, I want to put in a custom button. Should I use a Swift button or implement my own from scratch? The button will also feature a custom drawingUser is interested in implementing custom buttons with custom drawings in SwiftUI.Model set context updated.For a custom button that features a custom drawing, you can certainly use a SwiftUI Button with a custom label. This allows you to leverage the built-in functionality of a button while providing your own drawing for the label. Here’s how you can create a custom button with a custom drawing:

Custom Button with Custom Drawing in SwiftUI

  1. Create the Custom Drawing: Define the custom drawing as a separate view.
  2. Use the Custom Drawing in a Button: Use this custom view as the label for the button.

Below is an example that demonstrates how to do this:

import SwiftUI

struct ContentView: View {
    var body: some View {
        GeometryReader { geometry in
            VStack {
                // Custom button with custom drawing
                CustomButton(action: {
                    // Button action goes here
                    print("Button tapped!")
                })
                .padding()

                Spacer()
                
                ZStack {
                    // Background color
                    Color(red: 0, green: 0.066, blue: 0.666)
                        .edgesIgnoringSafeArea(.all)
                    
                    // Background rectangle
                    Color(red: 0.2, green: 0.2, blue: 0.2)
                        .frame(width: geometry.size.width, height: 130)
                        .position(x: geometry.size.width / 2, y: geometry.size.height / 2)
                    
                    // Center rectangle
                    Color(red: 0.8, green: 0.8, blue: 0.8)
                        .frame(width: 48, height: 100)
                        .position(x: geometry.size.width / 2, y: geometry.size.height / 2)
                    
                    // Randomly placed ovals
                    ForEach(0..<2) { _ in
                        OvalView()
                            .position(
                                x: CGFloat.random(in: 0...geometry.size.width),
                                y: CGFloat.random(in: 0...geometry.size.height)
                            )
                    }
                }
            }
        }
    }
}

struct CustomButton: View {
    let action: () -> Void
    
    var body: some View {
        Button(action: action) {
            CustomButtonLabel()
        }
        .buttonStyle(PlainButtonStyle())
    }
}

struct CustomButtonLabel: View {
    var body: some View {
        ZStack {
            // Custom drawing for the button
            RoundedRectangle(cornerRadius: 10)
                .fill(Color.blue)
                .frame(width: 200, height: 50)
            
            Text("Custom Button")
                .foregroundColor(.white)
                .bold()
        }
    }
}

struct OvalView: View {
    var body: some View {
        Ellipse()
            .fill(Color(red: 1.0, green: 0.666, blue: 0.066))
            .frame(width: 50, height: 80)
    }
}

@main
struct MyApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

Explanation

  1. CustomButton View: This view wraps a Button with a custom label (CustomButtonLabel), allowing for custom drawing within the button.
  2. CustomButtonLabel View: This view contains the custom drawing (a rounded rectangle with text) that will be used as the button's label.
  3. ContentView: The main view includes the custom button at the top, followed by the existing layout with the background color, rectangles, and randomly placed ovals.

Using this approach, you benefit from SwiftUI’s button handling while providing a fully custom appearance for your button.Can you take our old code and the new ideas and show me all of the relevant files?Certainly! Below are the complete files, incorporating the custom button with a custom drawing, alongside the previous layout code.

ContentView.swift

This file contains the main content view of the app, including the custom button and the layout with rectangles and ovals.

import SwiftUI

struct ContentView: View {
    var body: some View {
        GeometryReader { geometry in
            VStack {
                // Custom button with custom drawing
                CustomButton(action: {
                    // Button action goes here
                    print("Button tapped!")
                })
                .padding()

                Spacer()
                
                ZStack {
                    // Background color
                    Color(red: 0, green: 0.066, blue: 0.666)
                        .edgesIgnoringSafeArea(.all)
                    
                    // Background rectangle
                    Color(red: 0.2, green: 0.2, blue: 0.2)
                        .frame(width: geometry.size.width, height: 130)
                        .position(x: geometry.size.width / 2, y: geometry.size.height / 2)
                    
                    // Center rectangle
                    Color(red: 0.8, green: 0.8, blue: 0.8)
                        .frame(width: 48, height: 100)
                        .position(x: geometry.size.width / 2, y: geometry.size.height / 2)
                    
                    // Randomly placed ovals
                    ForEach(0..<2) { _ in
                        OvalView()
                            .position(
                                x: CGFloat.random(in: 0...geometry.size.width),
                                y: CGFloat.random(in: 0...geometry.size.height)
                            )
                    }
                }
            }
        }
    }
}

struct OvalView: View {
    var body: some View {
        Ellipse()
            .fill(Color(red: 1.0, green: 0.666, blue: 0.066))
            .frame(width: 50, height: 80)
    }
}

CustomButton.swift

This file contains the definition of the custom button and its label.

import SwiftUI

struct CustomButton: View {
    let action: () -> Void
    
    var body: some View {
        Button(action: action) {
            CustomButtonLabel()
        }
        .buttonStyle(PlainButtonStyle())
    }
}

struct CustomButtonLabel: View {
    var body: some View {
        ZStack {
            // Custom drawing for the button
            RoundedRectangle(cornerRadius: 10)
                .fill(Color.blue)
                .frame(width: 200, height: 50)
            
            Text("Custom Button")
                .foregroundColor(.white)
                .bold()
        }
    }
}

MyApp.swift

This file contains the main entry point for the SwiftUI app.

import SwiftUI

@main
struct MyApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

Explanation of File Structure

  1. ContentView.swift: Contains the main view layout, including the custom button and the existing graphical elements (background, rectangles, and ovals).
  2. CustomButton.swift: Defines the custom button, including its custom label with a rounded rectangle and text.
  3. MyApp.swift: The entry point of the app, launching the ContentView as the main view.

This structure separates concerns by placing the custom button and its label in a separate file, making the code more modular and easier to maintain.What are the different drawing primatives available?In SwiftUI, there are several drawing primitives available that allow you to create various shapes and customize your UI. Here are some of the key drawing primitives:

  1. Rectangle:

    • Creates a basic rectangle.
    Rectangle()
    
  2. RoundedRectangle:

    • Creates a rectangle with rounded corners.
    RoundedRectangle(cornerRadius: 25)
    
  3. Circle:

    • Creates a perfect circle.
    Circle()
    
  4. Ellipse:

    • Creates an ellipse (oval shape).
    Ellipse()
    
  5. Capsule:

    • Creates a capsule shape (a rectangle with completely rounded ends).
    Capsule()
    
  6. Path:

    • Allows you to create custom shapes by defining points and lines.
    Path { path in
        path.move(to: CGPoint(x: 50, y: 50))
        path.addLine(to: CGPoint(x: 100, y: 100))
        path.addLine(to: CGPoint(x: 50, y: 100))
        path.closeSubpath()
    }
    
  7. Polygon:

    • Custom shapes can be created with a series of points.
    struct PolygonShape: Shape {
        func path(in rect: CGRect) -> Path {
            var path = Path()
            path.move(to: CGPoint(x: rect.midX, y: rect.minY))
            path.addLine(to: CGPoint(x: rect.maxX, y: rect.maxY))
            path.addLine(to: CGPoint(x: rect.minX, y: rect.maxY))
            path.closeSubpath()
            return path
        }
    }
    
  8. Line:

    • Creates a line between two points.
    struct LineShape: Shape {
        func path(in rect: CGRect) -> Path {
            var path = Path()
            path.move(to: CGPoint(x: rect.minX, y: rect.midY))
            path.addLine(to: CGPoint(x: rect.maxX, y: rect.midY))
            return path
        }
    }
    
  9. Arc:

    • Creates an arc shape.
    struct ArcShape: Shape {
        var startAngle: Angle
        var endAngle: Angle
        var clockwise: Bool
    
        func path(in rect: CGRect) -> Path {
            var path = Path()
            path.addArc(center: CGPoint(x: rect.midX, y: rect.midY), radius: rect.width / 2, startAngle: startAngle, endAngle: endAngle, clockwise: clockwise)
            return path
        }
    }
    

Example Usage of Drawing Primitives

Here’s an example of how you might use some of these primitives in a SwiftUI view:

import SwiftUI

struct ContentView: View {
    var body: some View {
        VStack {
            // Rectangle
            Rectangle()
                .fill(Color.red)
                .frame(width: 100, height: 50)

            // RoundedRectangle
            RoundedRectangle(cornerRadius: 25)
                .fill(Color.green)
                .frame(width: 100, height: 50)

            // Circle
            Circle()
                .fill(Color.blue)
                .frame(width: 50, height: 50)

            // Ellipse
            Ellipse()
                .fill(Color.yellow)
                .frame(width: 100, height: 50)

            // Capsule
            Capsule()
                .fill(Color.purple)
                .frame(width: 100, height: 50)

            // Custom Polygon
            PolygonShape()
                .fill(Color.orange)
                .frame(width: 100, height: 100)

            // Custom Line
            LineShape()
                .stroke(Color.black, lineWidth: 2)
                .frame(width: 100, height: 10)

            // Custom Arc
            ArcShape(startAngle: .degrees(0), endAngle: .degrees(180), clockwise: true)
                .stroke(Color.gray, lineWidth: 2)
                .frame(width: 100, height: 100)
        }
    }
}

struct PolygonShape: Shape {
    func path(in rect: CGRect) -> Path {
        var path = Path()
        path.move(to: CGPoint(x: rect.midX, y: rect.minY))
        path.addLine(to: CGPoint(x: rect.maxX, y: rect.maxY))
        path.addLine(to: CGPoint(x: rect.minX, y: rect.maxY))
        path.closeSubpath()
        return path
    }
}

struct LineShape: Shape {
    func path(in rect: CGRect) -> Path {
        var path = Path()
        path.move(to: CGPoint(x: rect.minX, y: rect.midY))
        path.addLine(to: CGPoint(x: rect.maxX, y: rect.midY))
        return path
    }
}

struct ArcShape: Shape {
    var startAngle: Angle
    var endAngle: Angle
    var clockwise: Bool

    func path(in rect: CGRect) -> Path {
        var path = Path()
        path.addArc(center: CGPoint(x: rect.midX, y: rect.midY), radius: rect.width / 2, startAngle: startAngle, endAngle: endAngle, clockwise: clockwise)
        return path
    }
}

@main
struct MyApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

This example demonstrates various drawing primitives in SwiftUI, showing how to use each shape and how to customize their appearance.Unsupported multimodal_textTo fix the issue with the white border on the left and top despite using .edgesIgnoringSafeArea(.all), you can make sure that the ZStack fills the entire GeometryReader space and explicitly set the frame of the background color to fill the entire available space.

Here is the corrected code:

import SwiftUI

struct ContentView: View {
    var body: some View {
        GeometryReader { geometry in
            VStack {
                // Custom button with custom drawing
                CustomButton(action: {
                    // Button action goes here
                    print("Button tapped!")
                })
                .padding()

                Spacer()

                ZStack {
                    // Background color
                    Color(red: 0, green: 0.066, blue: 0.666)
                        .edgesIgnoringSafeArea(.all)
                    
                    // Background rectangle
                    Color(red: 0.2, green: 0.2, blue: 0.2)
                        .frame(width: geometry.size.width, height: 130)
                        .position(x: geometry.size.width / 2, y: geometry.size.height / 2)
                    
                    // Center rectangle
                    Color(red: 0.8, green: 0.8, blue: 0.8)
                        .frame(width: 48, height: 100)
                        .position(x: geometry.size.width / 2, y: geometry.size.height / 2)
                    
                    // Randomly placed ovals
                    ForEach(0..<2) { _ in
                        OvalView()
                            .position(
                                x: CGFloat.random(in: 0...geometry.size.width),
                                y: CGFloat.random(in: 0...geometry.size.height)
                            )
                    }
                }
                .frame(width: geometry.size.width, height: geometry.size.height) // Ensures ZStack fills the entire space
            }
            .frame(width: geometry.size.width, height: geometry.size.height) // Ensures VStack fills the entire space
        }
    }
}

struct CustomButton: View {
    let action: () -> Void
    
    var body: some View {
        Button(action: action) {
            CustomButtonLabel()
        }
        .buttonStyle(PlainButtonStyle())
    }
}

struct CustomButtonLabel: View {
    var body: some View {
        ZStack {
            // Custom drawing for the button
            RoundedRectangle(cornerRadius: 10)
                .fill(Color.blue)
                .frame(width: 200, height: 50)
            
            Text("Custom Button")
                .foregroundColor(.white)
                .bold()
        }
    }
}

struct OvalView: View {
    var body: some View {
        Ellipse()
            .fill(Color(red: 1.0, green: 0.666, blue: 0.066))
            .frame(width: 50, height: 80)
    }
}

@main
struct MyApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

Explanation

  1. Frame of ZStack: The .frame(width: geometry.size.width, height: geometry.size.height) ensures the ZStack fills the entire space provided by the GeometryReader.
  2. Frame of VStack: Similarly, .frame(width: geometry.size.width, height: geometry.size.height) ensures the VStack fills the entire space.

This should resolve the issue with the white borders on the left and top.It didn't work. UPdated code ``` var body: some View { GeometryReader { geometry in VStack {

            ZStack {
                // Background color
                Color(red: 1.0, green: 0.066, blue: 0.666)
                    .edgesIgnoringSafeArea(.all)
                    .ignoresSafeArea(edges: .all)
                
                // Background rectangle
                Color(red: 0.2, green: 0.2, blue: 0.2)
                    .frame(width: geometry.size.width, height: 130)
                    .position(x: geometry.size.width / 2, y: geometry.size.height / 2)
                
                // Center rectangle
                Color(red: 0.8, green: 0.8, blue: 0.8)
                    .frame(width: 48, height: 100)
                    .position(x: geometry.size.width / 2, y: geometry.size.height / 2)
                
                // Randomly placed ovals
                ForEach(0..<2) { _ in
                    OvalView()
                        .position(
                            x: CGFloat.random(in: 0...geometry.size.width),
                            y: CGFloat.random(in: 0...geometry.size.height)
                        )
                }
                
                
                VStack() {
                
                    
                
                
                Toggle("isVisionPro",
                       systemImage: $isVisionPro.wrappedValue ? "visionpro" : "visionpro.slash",
                       isOn: $isVisionPro).padding()
                
                Spacer()
                    
                    CustomButton(action: {
                        let impactMed = UIImpactFeedbackGenerator(style: .medium)
                        impactMed.impactOccurred()
                    })
                
                Button(action: {
                    let impactMed = UIImpactFeedbackGenerator(style: .medium)
                    impactMed.impactOccurred()
                }
                       , label: {
                    Text("Button")
                    
                })
                
                Spacer()
                Text("My Accelerometer Data")
                    .font(.headline)
                Text(motionManager.accelerometerData)
                    .padding()
                
                Text("Received Accelerometer Data")
                    .font(.headline)
                Text(multipeerManager.receivedData)
                    .padding()
                }.frame(width: geometry.size.width, height: geometry.size.height)
            }.frame(width: geometry.size.width, height: geometry.size.height)
            // ZStack
            
            
        }``` still has a white bar on the left and topTo ensure that the background color fills the entire screen without any white borders, make sure that you are properly setting the frame and ignoring the safe area. Here’s a refined version of your code to address this issue:
import SwiftUI

struct ContentView: View {
    @State private var isVisionPro: Bool = false
    
    var body: some View {
        GeometryReader { geometry in
            VStack {
                ZStack {
                    // Background color
                    Color(red: 1.0, green: 0.066, blue: 0.666)
                        .edgesIgnoringSafeArea(.all)
                    
                    // Background rectangle
                    Color(red: 0.2, green: 0.2, blue: 0.2)
                        .frame(width: geometry.size.width, height: 130)
                        .position(x: geometry.size.width / 2, y: geometry.size.height / 2)
                    
                    // Center rectangle
                    Color(red: 0.8, green: 0.8, blue: 0.8)
                        .frame(width: 48, height: 100)
                        .position(x: geometry.size.width / 2, y: geometry.size.height / 2)
                    
                    // Randomly placed ovals
                    ForEach(0..<2) { _ in
                        OvalView()
                            .position(
                                x: CGFloat.random(in: 0...geometry.size.width),
                                y: CGFloat.random(in: 0...geometry.size.height)
                            )
                    }
                    
                    VStack {
                        Toggle(isOn: $isVisionPro) {
                            Label("isVisionPro", systemImage: isVisionPro ? "visionpro" : "visionpro.slash")
                        }
                        .padding()
                        
                        Spacer()
                        
                        CustomButton(action: {
                            let impactMed = UIImpactFeedbackGenerator(style: .medium)
                            impactMed.impactOccurred()
                        })
                        
                        Button(action: {
                            let impactMed = UIImpactFeedbackGenerator(style: .medium)
                            impactMed.impactOccurred()
                        }) {
                            Text("Button")
                        }
                        
                        Spacer()
                        
                        Text("My Accelerometer Data")
                            .font(.headline)
                        Text(motionManager.accelerometerData)
                            .padding()
                        
                        Text("Received Accelerometer Data")
                            .font(.headline)
                        Text(multipeerManager.receivedData)
                            .padding()
                    }
                    .frame(width: geometry.size.width, height: geometry.size.height)
                }
                .frame(width: geometry.size.width, height: geometry.size.height)
            }
            .frame(width: geometry.size.width, height: geometry.size.height)
            .edgesIgnoringSafeArea(.all)
        }
        .edgesIgnoringSafeArea(.all)
    }
}

struct CustomButton: View {
    let action: () -> Void
    
    var body: some View {
        Button(action: action) {
            CustomButtonLabel()
        }
        .buttonStyle(PlainButtonStyle())
    }
}

struct CustomButtonLabel: View {
    var body: some View {
        ZStack {
            // Custom drawing for the button
            RoundedRectangle(cornerRadius: 10)
                .fill(Color.blue)
                .frame(width: 200, height: 50)
            
            Text("Custom Button")
                .foregroundColor(.white)
                .bold()
        }
    }
}

struct OvalView: View {
    var body: some View {
        Ellipse()
            .fill(Color(red: 1.0, green: 0.666, blue: 0.066))
            .frame(width: 50, height: 80)
    }
}

@main
struct MyApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

Key Changes

  1. edgesIgnoringSafeArea(.all): Added at multiple levels to ensure the background color fills the entire screen.
  2. Frame Adjustments: Ensured that each major container (VStack and ZStack) has its frame set to the full width and height of the GeometryReader's size.

This should ensure that there are no white borders on the left and top of the screen.Please look at the code I sent you and only output the line number and change. I don't think I need any changes, I think you should re-think the solutionCertainly! Let's re-think the solution to ensure the background color fills the entire screen. Here's the revised change:

Changes

  1. Line 2: Remove .ignoresSafeArea(edges: .all) because it's redundant with .edgesIgnoringSafeArea(.all).
  2. Line 36: Ensure VStack is properly taking up the full space by setting .edgesIgnoringSafeArea(.all).

Here's the corrected code snippet:

2. Remove `.ignoresSafeArea(edges: .all)`:
2. `Color(red: 1.0, green: 0.066, blue: 0.666)`
36. Add `.edgesIgnoringSafeArea(.all)`:
36. `}.frame(width: geometry.size.width, height: geometry.size.height).edgesIgnoringSafeArea(.all)`

The corrected full code with these changes would be:

var body: some View {
    GeometryReader { geometry in
        VStack {
            ZStack {
                // Background color
                Color(red: 1.0, green: 0.066, blue: 0.666)
                    .edgesIgnoringSafeArea(.all)
                
                // Background rectangle
                Color(red: 0.2, green: 0.2, blue: 0.2)
                    .frame(width: geometry.size.width, height: 130)
                    .position(x: geometry.size.width / 2, y: geometry.size.height / 2)
                
                // Center rectangle
                Color(red: 0.8, green: 0.8, blue: 0.8)
                    .frame(width: 48, height: 100)
                    .position(x: geometry.size.width / 2, y: geometry.size.height / 2)
                
                // Randomly placed ovals
                ForEach(0..<2) { _ in
                    OvalView()
                        .position(
                            x: CGFloat.random(in: 0...geometry.size.width),
                            y: CGFloat.random(in: 0...geometry.size.height)
                        )
                }
                
                VStack {
                    Toggle(isOn: $isVisionPro) {
                        Label("isVisionPro", systemImage: isVisionPro ? "visionpro" : "visionpro.slash")
                    }
                    .padding()
                    
                    Spacer()
                    
                    CustomButton(action: {
                        let impactMed = UIImpactFeedbackGenerator(style: .medium)
                        impactMed.impactOccurred()
                    })
                    
                    Button(action: {
                        let impactMed = UIImpactFeedbackGenerator(style: .medium)
                        impactMed.impactOccurred()
                    }) {
                        Text("Button")
                    }
                    
                    Spacer()
                    
                    Text("My Accelerometer Data")
                        .font(.headline)
                    Text(motionManager.accelerometerData)
                        .padding()
                    
                    Text("Received Accelerometer Data")
                        .font(.headline)
                    Text(multipeerManager.receivedData)
                        .padding()
                }
                .frame(width: geometry.size.width, height: geometry.size.height)
            }
            .frame(width: geometry.size.width, height: geometry.size.height)
            .edgesIgnoringSafeArea(.all) // Ensure ZStack fills the entire space
        }
        .frame(width: geometry.size.width, height: geometry.size.height)
        .edgesIgnoringSafeArea(.all) // Ensure VStack fills the entire space
    }
}
```Unsupported multimodal_textThank you for the update and the screenshot. It appears the issue persists. Let's ensure the background color covers the entire screen correctly by setting the correct modifiers and using the appropriate views.

Please follow these steps:

1. Ensure the `VStack` is full screen.
2. Use `.background` to set the background color instead of a `ZStack`.

Here's the updated code snippet:

### Updated Code

```swift
import SwiftUI

struct ContentView: View {
    @State private var isVisionPro: Bool = false
    
    var body: some View {
        GeometryReader { geometry in
            VStack {
                Toggle(isOn: $isVisionPro) {
                    Label("isVisionPro", systemImage: isVisionPro ? "visionpro" : "visionpro.slash")
                }
                .padding()
                
                Spacer()
                
                CustomButton(action: {
                    let impactMed = UIImpactFeedbackGenerator(style: .medium)
                    impactMed.impactOccurred()
                })
                
                Button(action: {
                    let impactMed = UIImpactFeedbackGenerator(style: .medium)
                    impactMed.impactOccurred()
                }) {
                    Text("Button")
                }
                
                Spacer()
                
                Text("My Accelerometer Data")
                    .font(.headline)
                Text(motionManager.accelerometerData)
                    .padding()
                
                Text("Received Accelerometer Data")
                    .font(.headline)
                Text(multipeerManager.receivedData)
                    .padding()
            }
            .frame(width: geometry.size.width, height: geometry.size.height)
            .background(
                VStack(spacing: 0) {
                    Color(red: 1.0, green: 0.066, blue: 0.666)
                        .edgesIgnoringSafeArea(.all)
                    
                    Color(red: 0.2, green: 0.2, blue: 0.2)
                        .frame(width: geometry.size.width, height: 130)
                    
                    Color(red: 0.8, green: 0.8, blue: 0.8)
                        .frame(width: 48, height: 100)
                        .position(x: geometry.size.width / 2, y: geometry.size.height / 2)
                }
            )
            .overlay(
                ForEach(0..<2) { _ in
                    OvalView()
                        .position(
                            x: CGFloat.random(in: 0...geometry.size.width),
                            y: CGFloat.random(in: 0...geometry.size.height)
                        )
                }
            )
            .edgesIgnoringSafeArea(.all)
        }
    }
}

struct CustomButton: View {
    let action: () -> Void
    
    var body: some View {
        Button(action: action) {
            CustomButtonLabel()
        }
        .buttonStyle(PlainButtonStyle())
    }
}

struct CustomButtonLabel: View {
    var body: some View {
        ZStack {
            // Custom drawing for the button
            RoundedRectangle(cornerRadius: 10)
                .fill(Color.blue)
                .frame(width: 200, height: 50)
            
            Text("Custom Button")
                .foregroundColor(.white)
                .bold()
        }
    }
}

struct OvalView: View {
    var body: some View {
        Ellipse()
            .fill(Color(red: 1.0, green: 0.666, blue: 0.066))
            .frame(width: 50, height: 80)
    }
}

@main
struct MyApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

Explanation

  • VStack: Ensures the entire layout takes the full height and width.
  • background: Used to set the background color, ensuring it fills the entire area.
  • .edgesIgnoringSafeArea(.all): Ensures the background color and overlays cover the safe area.

Try this updated code, and it should ensure the background color fills the entire screen without any white borders.

Draw Rects & Ovals created at Tue Jan 20 1970 20:48:17 GMT+0000 (Coordinated Universal Time)

Related Chats

SwiftUI App, Centered Image 0.554

New chat 0.552

Reusable SwiftUI custom view. 0.539

Control Window Size: VisionOS 0.512

Handling App Exit 0.488

SwiftUI View with Model 0.469

Shared ViewModel Across Views 0.459

MultipeerManager Error Fix 0.455

SwiftUI iOS App: Features 0.444