Skip to content

Instantly share code, notes, and snippets.

@vakrilov
Created February 23, 2018 09:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vakrilov/7e5d24ec8873b1df710012e6a9b3294b to your computer and use it in GitHub Desktop.
Save vakrilov/7e5d24ec8873b1df710012e6a9b3294b to your computer and use it in GitHub Desktop.
Notify Layout Event Implementation
import { StackLayout } from "tns-core-modules/ui/layouts/stack-layout";
import { isAndroid, isIOS } from "tns-core-modules/platform";
import { View } from "tns-core-modules/ui/core/view";
import { registerElement } from "nativescript-angular/element-registry";
export class NotifyLayoutView extends StackLayout {
private listener: android.view.View.OnLayoutChangeListener
public createNativeView() {
const nativeView = super.createNativeView();
if (isAndroid) {
const owner = this;
this.listener = new android.view.View.OnLayoutChangeListener({
onLayoutChange(
v: android.view.View,
left: number, top: number, right: number, bottom: number,
oldLeft: number, oldTop: number, oldRight: number, oldBottom: number): void {
owner.notifyLayoutChanged();
}
});
(<android.view.View>nativeView).addOnLayoutChangeListener(this.listener);
}
return nativeView;
}
onLayout(left: number, top: number, right: number, bottom: number): void {
super.onLayout(left, top, right, bottom);
if (isIOS) {
this.notifyLayoutChanged();
}
}
notifyLayoutChanged() {
this.notify({ object: this, eventName: "layout" });
}
}
// Register tag in the ns-angular renderer
registerElement("notify-layout-view", () => NotifyLayoutView);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment