UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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 Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

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

Reply to this topic…

You need to create an account or log in to reply.

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.