Safe area with iPhone Duo in Split View

Hi.

When using the brand new iPhone Duo simulator I found a weird thing. When my app is running on the left side in a split view, the tab bar is appearing on the left side. But when I print the safe area of the window, it showed

▿ UIEdgeInsets
  - top : 0.0
  - left : 0.0
  - bottom : 34.0
  - right : 0.0

But isn't the left inset supposed to be 469-385 = 84?

When it's running on the right side, the safe area insets seems to be correct:

▿ UIEdgeInsets
  - top : 0.0
  - left : 0.0
  - bottom : 34.0
  - right : 84.0

Here's how I get the safe area insets. Please correct me if I'm using a wrong way.

extension UIApplication {
    var currentScene: UIWindowScene? {
        return connectedScenes.first as? UIWindowScene
    }
}

extension UIApplication {
    var currentKeyWindow: UIWindow? {
        currentScene?.windows.filter { $0.isKeyWindow }.first
    }
}

// Somewhere in my code
static var safeAreaInsets: UIEdgeInsets {
    let window = UIApplication.shared.currentKeyWindow
    let insets = window?.safeAreaInsets ?? .zero
    return insets
}

Thank you guys so much for reading.

I guess you should better use the safeAreaInsets property from the view of your view controller, not from the window. From what I've seen so far, the view does report back the correct safeArea values.

The window might also need to take other aspects into account (like the camera and rounded corners etc) than the views within view controllers, therefore you get different safeAreas.

I would have to verify, but elementarteilchen has the right idea – always check for UI layout information from the nearest relevant view, rather than from a root location like the window. Since the tab bar is deeper in the hierarchy than the window, it won't have impact on the window's safe area.

Additionally, we would strongly advise against code like you presented above for multiple reasons:

  1. There is no "current scene" concept – all scenes in your application are considered equal, generally only differentiated based on their activation level. If your application ends up supporting multiple scenes, this code will not reliably find a specific scene. If it does not, we would recommend obtaining scene references from a window instead.
  2. There is no "current key window" concept for similar reasons. There may be multiple windows in a scene, and those windows may not have been created by you. We would recommend you keep track of your own windows for this reason.
  3. Statically accessing values like safeAreaInsets that are strongly tied to layout at arbitrary times will generally only invite complexity and bugs into your code base. If you need to reference layout derived values, you should access them during layout (triggering a layout pass if needed).

I also noticed that the safe area insets for the inner screen are incorrect. Please provide an updated simulator.