Safeareainsets in Uiview Is 0 on an iPhone X

safeAreaInsets in UIView is 0 on an iPhone X

I already figure out the solution: I was doing all the implementation in the init of the view. safeAreaInsets has the correct size in layoutSubviews()

SafeAreaInsets are 0 when View.OnAppearing gets triggered

For the ViewSafeAreaInsetsDidChange method, it gets called from the PageRenderer. You could override the default iOS PageRenderer and hook up to the ViewSafeAreaInsetsDidChange override.

    public override void ViewSafeAreaInsetsDidChange()
{

var page = (Element as Page);
if (page != null && Forms.IsiOS11OrNewer)
{
var insets = NativeView.SafeAreaInsets;
if(page.Parent is TabbedPage)
{
insets.Bottom = 0;
}
page.On<PlatformConfiguration.iOS>().SetSafeAreaInsets(new Thickness(insets.Left, insets.Top, insets.Right, insets.Bottom));

}
base.ViewSafeAreaInsetsDidChange();
}

With more details, you could check the link below. https://github.com/xamarin/Xamarin.Forms/blob/4d49e5786a4c9cfc6eea00bbaa5b57bc09679186/Xamarin.Forms.Platform.iOS/Renderers/PageRenderer.cs

How to get safeAreaInsets in viewWillTransitionToSize (iPhone X)

I got the valid solution at Stackoverflow Japan.

It's just getting the insets in the UIViewControllerTransitionCoordinator.animate(alongsideTransition:completion:) closure like below:

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)

coordinator.animate(alongsideTransition: { (context) in
...

let insets = ...safeAreaInsets

...
}, completion: nil)
}

safeAreaInsets return wrong values in viewDidLoad, where's the best place to get it?

You need to use viewDidLayoutSubviews instead of viewDidLoad.

override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
// your layout code here
}

Note: Unlike viewDidLoad which is only called once, viewDidLayoutSubviews is often called multiple times. Make sure to only set the frames inside viewDidLayoutSubviews, and don't perform operations that should only happen once, like adding subviews.

Calculate Height of UIView with Safe Area Guides for iPhone X

Try this one. Check the phone is iPhoneX or Not (Top padding greater than 0 if iPhoneX)

if (@available(iOS 11.0, *)) {
UIWindow *window = UIApplication.sharedApplication.keyWindow;
CGFloat topPadding = window.safeAreaInsets.top;
self.headerViewHeight.constant = (topPadding>0) ? 46+topPadding : 60;
}

Get safe area inset top and bottom heights

Try this :

In Objective C

if (@available(iOS 11.0, *)) {
UIWindow *window = UIApplication.sharedApplication.windows.firstObject;
CGFloat topPadding = window.safeAreaInsets.top;
CGFloat bottomPadding = window.safeAreaInsets.bottom;
}

In Swift

if #available(iOS 11.0, *) {
let window = UIApplication.shared.keyWindow
let topPadding = window?.safeAreaInsets.top
let bottomPadding = window?.safeAreaInsets.bottom
}

In Swift - iOS 13.0 and above

// Use the first element from windows array as KeyWindow deprecated

if #available(iOS 13.0, *) {
let window = UIApplication.shared.windows.first
let topPadding = window.safeAreaInsets.top
let bottomPadding = window.safeAreaInsets.bottom
}

UIViewControllerAnimatedTransitioning with Safe Area Insets on iPhone X

Ensure that the destination view controller is actually in the view hierarchy when you call layoutIfNeeded. I had this same problem, oddly only affecting certain orientations, and fixed it by adding the second and third lines below:

UIViewController * toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];

// Added these two lines:
toViewController.view.frame = [transitionContext finalFrameForViewController:toViewController];
[transitionContext.containerView addSubview:toViewController.view];

[toViewController.view setNeedsLayout];
[toViewController.view layoutIfNeeded];


Related Topics



Leave a reply



Submit