fix(ios): safeAreaLayoutGuide fallback for iOS 10 cases (#5960)

This commit is contained in:
Alexander Vakrilov
2018-06-20 07:28:06 +03:00
committed by GitHub
parent d79ac300f3
commit 4b5754a6d4

View File

@@ -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 = (<any>root).safeAreaLayoutGuide = UILayoutGuide.alloc().init();
root.addLayoutGuide(layoutGuide);
NSLayoutConstraint.activateConstraints(<any>[
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);
(<any>controller.view).safeAreaLayoutGuide = layoutGuide;
}
}
function initLayoutGuide(controller: UIViewController) {
const rootView = controller.view;
const layoutGuide = UILayoutGuide.alloc().init();
rootView.addLayoutGuide(layoutGuide);
NSLayoutConstraint.activateConstraints(<any>[
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;