Safe area not work properly on iPhone X when subviews are not in the view area

iphone x safe area size
uitabbarcontroller safe area
uitableview safe area
child view controller safe area
contentinsetadjustmentbehavior
ios 13 safe area
ios safe area programmatically
ios safe area insets

I have a scrollView contains 2 child viewController. You can see VC2 not layout properly.

I found if view is not yet visible onscreen. safeAreaInsets is always 0.

I can call vc2.view.setNeedsLayout() to fix this problem when scroll ended. But the layout is not correct until scroll ended.

The document says

If the view is not currently installed in a view hierarchy, or is not yet visible onscreen, the edge insets in this property are 0.

So how can I fix this situation.

Autolayout

instead of referencing the current view's safeAreaInsets, set it to the UIApplication:

(UIApplication.shared.delegate?.window??.safeAreaInsets.bottom)

ios, I have a scrollView contains 2 child viewController. You can see VC2 not layout properly. I found if view is not yet visible onscreen. safeAreaInsets is always 0. Subviews are attached to the view’s edges. Labels are attached to the superview safe area. All safe area insets are calculated properly but the navigation bar is moved to the top under the notch. A really sad bug and as I know there are no ways to fix it for now excepting some workarounds.

In your child view controllers if you set the view controllers additionalSafeAreaInsets equal to the window's safe area insets they will layout correctly respecting the safe areas.

I found I had to do this inside of viewDidLoad() and viewWillTransition(to size: CGSize, with coordinator: UIVIewControllerTransitionCoordinator

Inside of viewWillTransition you will want to set the additionalSafeAreaInsets in the animation block of the coordinator:

coordinator.animate(alongsideTransition: { _ in
    if #available(iOS 11.0, *) {
        self.additionalSafeAreaInsets = UIApplication.shared.delegate?.window??.safeAreaInsets
    }
}, completion: nil)

Positioning Content Relative to the Safe Area, Position views so that they are not obstructed by other content. In tvOS, the safe area also includes the screen's overscan insets, which Suppose you define a container view controller that displays custom views along the bottom and right edges of the macOS · iOS · watchOS · tvOS · Safari and Web · Games · Business  safeAreaInsets is always 0. I can call vc2.view.setNeedsLayout() to fix this problem when scroll ended. But the layout is not correct until scroll ended. The document says. If the view is not currently installed in a view hierarchy, or is not yet visible onscreen, the edge insets in this property are 0.

I was building a custom paging view controller and ran into this issue as well @PowHU.

The only solution that seemed to work for me was to set the view controller's view class in the storyboard to a custom class I created called AlwaysSafeAreaInsetsView.

import UIKit

class AlwaysSafeAreaInsetsView: UIView {

    @available(iOS 11.0, *)
    override var safeAreaInsets: UIEdgeInsets {
        if let window = UIApplication.shared.keyWindow {
            return window.safeAreaInsets
        }
        return super.safeAreaInsets
    }

}

iOS Safe Area - @RosberryApps, The iPhone X has the top and the bottom safe area insets in portrait orientation. safe area not only in a view controller but anywhere in the subviews hierarchy. It works great but a strange thing happens when the status bar is hidden All safe area insets are calculated properly but the navigation bar is  For iPhone X users and above, swipe up from the bottom of the screen and press on the screen when your finger is near the center to open multitasking view, and then swipe up on Safari to close it. 2. To shut down your phone, press and hold the sleep/wake button until a slider appears. Slide to the right to power off your phone.

If I see properly your container view is pinned to the top and bottom of it's superview. Pin it to the Safe Area, and your child view controllers will be laid out properly.

Programming iOS 13: Dive Deep into Views, View Controllers, and , portrait orientation, this view will still be right up against the top of the screen — which On a device without a bezel, such as the iPhone X, the safe area is of even concerned to position subviews of a view controller's main view with respect to But any view — not just the view controller's main view — can participate in  Take a look at the safe area (blue area) for the iPhone X: Portrait: Landscape: And this is the safe area on the iPhone 8: As you can see, on the iPhone 8 the safe area is basically the whole screen without the status bar. So if you are using auto layout and place your content within the safe area, your app will be displayed properly on all devices.

The Particulars of the Safe Area and , I won't actually use the iPhone X too much, as I think we can learn more from the above, you've got a pretty decent grasp on how modern layout works on iOS. The safe area of a view reflects the area not covered by navigation bars, tab Right: one view, showing status bar, showing navigation bar. The new Apple iPhone X troubleshooting guide Battery life is also impacted by switching between different mobile coverage areas, for example: If you are in an area with a poor mobile signal

Layout Guide, Margins, Insets and Safe Area demystified on iOS 10 , It's time to review and demystify margins, insets, scroll views and Safe Area. to subviews, but the system ( iOS ) enforce margins of the view controller's root But on iOS >= 11 , expected left and right margins are enabled by this same Maybe you were not using layoutMargins until iOS 11 , but directionalLayoutMargins  The safe and unsafe areas on iPhone X in the landscape orientation, with insets indicated. To achieve this, WebKit in iOS 11 includes a new CSS function , env() , and a set of four pre-defined environment variables , safe-area-inset-left , safe-area-inset-right , safe-area-inset-top , and safe-area-inset-bottom .

iOS Apps for Masterminds 4th Edition: How to take advantage of , The position of the views in the content area is not determined by constraints but by the After the content area is ready, the Image View is added as a subview of the Scroll View with the the Safe Area. Run the application and scroll the image. addSubview(imgView) let logoView = UIImageView(frame: CGRect(x: 25,  The most recent iPhone devices, such as the iPhone X, XS, and XR, don't have the system button anymore and provide a larger screen area. When you build apps for these devices, you must now consider this larger screen area in order to ensure your contents properly fit into the page.

Comments
  • You need to provide more information, show us what the constraints of "VC1 Bottom" and "VC2 Bottom" look like. Are they exactly the same?
  • Yes.They are same constraints center to safe area & bottom to safe area.
  • Have you tired to put vc2.view.setNeedsLayout() in the viewDidLayoutSubviews() or something like that? (with a proper test to prevent a potential infinite loop)
  • viewDidLayoutSubviews() is the same. When view is not onscreen safe area is always 0.
  • Find any solution for this? I have the same problem and targeting iOS10 I can't use safeAreaInsets
  • safeAreaInsets is only available targeting iOS 11.0 or newer
  • No. I want container view is the same size as superView.