From 4b5754a6d4b532943c284753e7848ccbc3c783e1 Mon Sep 17 00:00:00 2001 From: Alexander Vakrilov Date: Wed, 20 Jun 2018 07:28:06 +0300 Subject: [PATCH] fix(ios): safeAreaLayoutGuide fallback for iOS 10 cases (#5960) --- tns-core-modules/ui/core/view/view.ios.ts | 35 ++++++++++++++++------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/tns-core-modules/ui/core/view/view.ios.ts b/tns-core-modules/ui/core/view/view.ios.ts index d4b139b7f..72b0830a1 100644 --- a/tns-core-modules/ui/core/view/view.ios.ts +++ b/tns-core-modules/ui/core/view/view.ios.ts @@ -4,7 +4,7 @@ import { ViewBase } from "../view-base"; import { ViewCommon, layout, isEnabledProperty, originXProperty, originYProperty, automationTextProperty, isUserInteractionEnabledProperty, - traceEnabled, traceWrite, traceCategories + traceEnabled, traceWrite, traceCategories, traceMessageType } from "./view-common"; import { ios as iosBackground, Background } from "../../styling/background"; @@ -615,17 +615,24 @@ export namespace ios { export function updateConstraints(controller: UIViewController, owner: View): void { const root = controller.view; if (!root.safeAreaLayoutGuide) { - const layoutGuide = (root).safeAreaLayoutGuide = UILayoutGuide.alloc().init(); - root.addLayoutGuide(layoutGuide); - NSLayoutConstraint.activateConstraints([ - layoutGuide.topAnchor.constraintEqualToAnchor(controller.topLayoutGuide.bottomAnchor), - layoutGuide.bottomAnchor.constraintEqualToAnchor(controller.bottomLayoutGuide.topAnchor), - layoutGuide.leadingAnchor.constraintEqualToAnchor(root.leadingAnchor), - layoutGuide.trailingAnchor.constraintEqualToAnchor(root.trailingAnchor) - ]); + const layoutGuide = initLayoutGuide(controller); + (controller.view).safeAreaLayoutGuide = layoutGuide; } } + function initLayoutGuide(controller: UIViewController) { + const rootView = controller.view; + const layoutGuide = UILayoutGuide.alloc().init(); + rootView.addLayoutGuide(layoutGuide); + NSLayoutConstraint.activateConstraints([ + layoutGuide.topAnchor.constraintEqualToAnchor(controller.topLayoutGuide.bottomAnchor), + layoutGuide.bottomAnchor.constraintEqualToAnchor(controller.bottomLayoutGuide.topAnchor), + layoutGuide.leadingAnchor.constraintEqualToAnchor(rootView.leadingAnchor), + layoutGuide.trailingAnchor.constraintEqualToAnchor(rootView.trailingAnchor) + ]); + return layoutGuide; + } + function getStatusBarHeight(viewController?: UIViewController): number { const app = iosUtils.getter(UIApplication, UIApplication.sharedApplication); if (!app || app.statusBarHidden) { @@ -646,7 +653,15 @@ export namespace ios { const frame = controller.view.frame; const fullscreenOrigin = frame.origin; const fullscreenSize = frame.size; - const safeArea = controller.view.safeAreaLayoutGuide.layoutFrame; + + let layoutGuide = controller.view.safeAreaLayoutGuide; + if (!layoutGuide) { + traceWrite(`safeAreaLayoutGuide during layout of ${owner}. Creating fallback constraints, but layout might be wrong.`, + traceCategories.Layout, traceMessageType.error); + + layoutGuide = initLayoutGuide(controller); + } + const safeArea = layoutGuide.layoutFrame; const safeOrigin = safeArea.origin; const safeAreaSize = safeArea.size;