Skip to content

Commit

Permalink
fix(layouts): Set automaticallyAdjustsScrollViewInsets (#5311)
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Vakrilov authored and SvetoslavTsenov committed Jan 23, 2018
1 parent 58d61ca commit b492996
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 67 deletions.
2 changes: 2 additions & 0 deletions tns-core-modules/ui/core/view/view.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,8 @@ export const isEnabledProperty: Property<View, boolean>;
export const isUserInteractionEnabledProperty: Property<View, boolean>;

export namespace ios {
export function isContentScrollable(controller: any /* UIViewController */, owner: View): boolean
export function updateAutoAdjustScrollInsets(controller: any /* UIViewController */, owner: View): void
export function updateConstraints(controller: any /* UIViewController */, owner: View): void;
export function layoutView(controller: any /* UIViewController */, owner: View): void;
export class UILayoutViewController {
Expand Down
130 changes: 65 additions & 65 deletions tns-core-modules/ui/core/view/view.ios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -575,21 +575,26 @@ export class CustomLayoutView extends View {
}
}

function isScrollable(controller: UIViewController, owner: View): boolean {
let scrollable = (<any>owner).scrollableContent;
if (scrollable === undefined) {
const view: UIView = controller.view.subviews.count > 0 ? controller.view.subviews[0] : null;
if (view instanceof UIScrollView) {
scrollable = true;
const majorVersion = iosUtils.MajorVersion;

export namespace ios {
export function isContentScrollable(controller: UIViewController, owner: View): boolean {
let scrollableContent = (<any>owner).scrollableContent;
if (scrollableContent === undefined) {
const view: UIView = controller.view.subviews.count > 0 ? controller.view.subviews[0] : null;
if (view instanceof UIScrollView) {
scrollableContent = true;
}
}
}

return scrollable === true || scrollable === "true";;
}
return scrollableContent === true || scrollableContent === "true";;
}

const majorVersion = iosUtils.MajorVersion;
export function updateAutoAdjustScrollInsets(controller: UIViewController, owner: View): void {
const scrollable = isContentScrollable(controller, owner);
controller.automaticallyAdjustsScrollViewInsets = scrollable;
}

export namespace ios {
export function updateConstraints(controller: UIViewController, owner: View): void {
const root = controller.view;
if (!root.safeAreaLayoutGuide) {
Expand Down Expand Up @@ -619,67 +624,56 @@ export namespace ios {
}

export function layoutView(controller: UIViewController, owner: View): void {
const fullscreen = controller ===
iosUtils.getter(UIApplication, UIApplication.sharedApplication).keyWindow.rootViewController;

let left: number, top: number, width: number, height: number;

const frame = controller.view.frame;
const fullscreenOrigin = frame.origin;
const fullscreenSize = frame.size;

if (fullscreen) {
left = layout.toDevicePixels(fullscreenOrigin.x);
top = layout.toDevicePixels(fullscreenOrigin.y);
width = layout.toDevicePixels(fullscreenSize.width);
height = layout.toDevicePixels(fullscreenSize.height);
const safeArea = controller.view.safeAreaLayoutGuide.layoutFrame;
const safeOrigin = safeArea.origin;
const safeAreaSize = safeArea.size;

const navController = controller.navigationController;
const navBarHidden = navController ? navController.navigationBarHidden : true;
const scrollable = isContentScrollable(controller, owner);
const hasChildControllers = controller.childViewControllers.count > 0;

const safeAreaTopLength = safeOrigin.y - fullscreenOrigin.y;
const safeAreaBottomLength = fullscreenSize.height - safeAreaSize.height - safeAreaTopLength;

if (!(controller.edgesForExtendedLayout & UIRectEdge.Top)) {
const statusBarHeight = getStatusBarHeight(controller);
const navBarHeight = controller.navigationController ? controller.navigationController.navigationBar.frame.size.height : 0;
fullscreenOrigin.y = safeOrigin.y;
fullscreenSize.height -= (statusBarHeight + navBarHeight);
}

left = safeOrigin.x;
width = safeAreaSize.width;

if (hasChildControllers) {
// If not inner most extend to fullscreen
top = fullscreenOrigin.y;
height = fullscreenSize.height;
} else if (!scrollable) {
// If not scrollable dock under safe area
top = safeOrigin.y;
height = safeAreaSize.height;
} else if (navBarHidden) {
// If scrollable but no navigation bar dock under safe area
top = safeOrigin.y;
height = navController ? (fullscreenSize.height - top) : safeAreaSize.height;
} else {
const safeArea = controller.view.safeAreaLayoutGuide.layoutFrame;
const safeOrigin = safeArea.origin;
const safeAreaSize = safeArea.size;

const navController = controller.navigationController;
const navBarHidden = navController ? navController.navigationBarHidden : true;
const scrollable = isScrollable(controller, owner);
const hasChildControllers = controller.childViewControllers.count > 0;

const safeAreaTopLength = safeOrigin.y - fullscreenOrigin.y;
const safeAreaBottomLength = fullscreenSize.height - safeAreaSize.height - safeAreaTopLength;

if (!(controller.edgesForExtendedLayout & UIRectEdge.Top)) {
const statusBarHeight = getStatusBarHeight(controller);
const navBarHeight = controller.navigationController ? controller.navigationController.navigationBar.frame.size.height : 0;
fullscreenOrigin.y = safeOrigin.y;
fullscreenSize.height -= (statusBarHeight + navBarHeight);
}

left = safeOrigin.x;
width = safeAreaSize.width;

if (hasChildControllers) {
// If not inner most extend to fullscreen
top = fullscreenOrigin.y;
height = fullscreenSize.height;
} else if (!scrollable) {
// If not scrollable dock under safe area
top = safeOrigin.y;
height = safeAreaSize.height;
} else if (navBarHidden) {
// If scrollable but no navigation bar dock under safe area
top = safeOrigin.y;
height = navController ? (fullscreenSize.height - top) : safeAreaSize.height;
} else {
// If scrollable and navigation bar extend to fullscreen
top = fullscreenOrigin.y;
height = fullscreenSize.height;
}

left = layout.toDevicePixels(left);
top = layout.toDevicePixels(top);
width = layout.toDevicePixels(width);
height = layout.toDevicePixels(height);
// If scrollable and navigation bar extend to fullscreen
top = fullscreenOrigin.y;
height = fullscreenSize.height;
}

left = layout.toDevicePixels(left);
top = layout.toDevicePixels(top);
width = layout.toDevicePixels(width);
height = layout.toDevicePixels(height);

const widthSpec = layout.makeMeasureSpec(width, layout.EXACTLY);
const heightSpec = layout.makeMeasureSpec(height, layout.EXACTLY);

Expand Down Expand Up @@ -736,7 +730,13 @@ export namespace ios {
public viewWillAppear(animated: boolean): void {
super.viewWillAppear(animated);
const owner = this.owner.get();
if (owner && !owner.parent) {
if(!owner){
return;
}

updateAutoAdjustScrollInsets(this, owner);

if (!owner.parent) {
owner.callLoaded();
}
}
Expand Down
5 changes: 4 additions & 1 deletion tns-core-modules/ui/page/page.ios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ class UIViewControllerImpl extends UIViewController {
frame._updateActionBar(owner);
}

// Set autoAdjustScrollInsets in will appear - as early as possible
iosView.updateAutoAdjustScrollInsets(this, owner);

// Pages in backstack are unloaded so raise loaded here.
if (!owner.isLoaded) {
owner.callLoaded();
Expand Down Expand Up @@ -419,4 +422,4 @@ function invalidateTopmostController(controller: UIViewController): void {
invalidateTopmostController(childController);
}
}
}
}
8 changes: 7 additions & 1 deletion tns-core-modules/ui/tab-view/tab-view.ios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,13 @@ class UITabBarControllerImpl extends UITabBarController {
public viewWillAppear(animated: boolean): void {
super.viewWillAppear(animated);
const owner = this._owner.get();
if (owner && !owner.parent) {
if(!owner){
return;
}

iosView.updateAutoAdjustScrollInsets(this, owner);

if (!owner.parent) {
owner.callLoaded();
}
}
Expand Down

0 comments on commit b492996

Please sign in to comment.