GO FURTHER, FASTER: Try the Swift Career Accelerator today! >>

SwiftUI: .ignoresSafeArea() not working?

Forums > 100 Days of SwiftUI

I have the following code:

import SwiftUI

@main
struct MyApp: App {
    var body: some Scene {
        WindowGroup {
            GeometryReader { geometry in
                LocationPickerView()
                    .frame(maxWidth: geometry.size.width, maxHeight: geometry.size.height)
                    .ignoresSafeArea()
            }
        }
    }
}

My goal is to have my LocationPickerView() take up the entire screen regardless of the orientation of the device. A LocationPickerView() is a custom view I built:

struct LocationView: UIViewRepresentable {
    func makeUIView(context: Context) -> ARView {
        let arView = ARView()

        return arView
    }

    func updateUIView(_ uiView: ARView, context: Context) { }
}

However, I have a problem. The safe area is not being ignored. At first glance, I thought this was because the frame was already set by a "constant" value from geometry.

So, I thought I would flip the order of when I ignore the safe area and when I set the frame. This only resulted in a much weirder configuration. At first, the view does take up the entire screen. However, when I change the device to another orientation, the view stays the original height and width it was without updating, despite using GeometryReader.

What is happening here? I am certain this is valid behavior, but I don't understand why it is and how to fix it. I am trying to make things as simple as possible and thought I would add the frame and safe area options in my main struct, in an attempt to keep that code out of every view.

This feels like an easy fix, but I can't figure it out.

2      

Probably all depends on what you have inside your LocationPickerView.

Simple code like below handles everything without a hitch.

struct ContentView: View {
    var body: some View {
        MyCustomView()
            .ignoresSafeArea()
    }
}

struct MyCustomView: UIViewRepresentable {
    func makeUIView(context: Context) -> some UIView {
        let view = UIView()
        view.backgroundColor = UIColor(.blue)
        return view
    }

    func updateUIView(_ uiView: UIViewType, context: Context) { }
}

2      

I think it has something to do with ARView. I tested your code and it does indeed work. However, it doesn't seem to work with what I have. I will look into it more, but this is kind of odd.

2      

2      

It to do with GeometryReader which take up the whole View therefore .ignoresSafeArea() is ignore.

2      

@NigelGee I think my situation using ARView is a little bit more complicated than that, unfortunately. Take a look at this post I found:

https://developer.apple.com/forums/thread/665031

2      

@joenaveau Check out Understanding frames and coordinates inside GeometryReader. It might help you understand what happen with it, and then you can work out with the ARView.

2      

Hacking with Swift is sponsored by Alex.

SPONSORED Alex is the iOS & Mac developer’s ultimate AI assistant. It integrates with Xcode, offering a best-in-class Swift coding agent. Generate modern SwiftUI from images. Fast-apply suggestions from Claude 3.5 Sonnet, o3-mini, and DeepSeek R1. Autofix Swift 6 errors and warnings. And so much more. Start your 7-day free trial today!

Try for free!

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.