通过设置Page的actionBarHidden属性,可以显式地控制ActionBar的可见性,如下所示:
import {Page} from "ui/page";
export class AppComponent {
constructor(page: Page) {
page.actionBarHidden = true;
}
}
actionBarHidden = "true"
即可。<Page xmlns="http://www.nativescript.org/tns.xsd" loaded="pageLoaded" actionBarHidden="true">
</Page>
这是在您的NativeScript Angular TypeScript组件中隐藏ActionBar的代码。
import { Component, OnInit } from "@angular/core";
import { Page } from "tns-core-modules/ui/page";
export class AppComponent implements OnInit {
constructor(private page: Page) {
}
ngOnInit(): void {
this.page.actionBarHidden = true;
}
}
<page-router-outlet actionBarVisibility="never"></page-router-outlet>
如果你正在使用 Angular,并且你的 HTML 中没有使用 page
,或者你正在使用模块的惰性加载,或者你有多个 page-router-outlet
,那么你可以利用指令。
创建一个新的指令:
hideActionBar.ts
import { Directive } from '@angular/core';
import { Page } from 'tns-core-modules/ui/page/page';
@Directive({
selector: '[hideActionBar]'
})
export class HideActionBarDirective {
constructor(private page: Page) {
this.page.actionBarHidden = true;
}
}
并在需要隐藏actionbar的HTML中使用此指令。
SecondPage.html
<GridLayout tkExampleTitle tkToggleNavButton rows="auto,*" hideActionBar>
...// other html goes here
</GridLayout>
P.S. 别忘了在 NgModule 中声明它,因为指令是可声明的 declarables。这对于代码共享项目非常有用,因为您将在 ngmodule.tns.ts 中声明它,并且不会编译为 Web 项目。
declarations: [
AppComponent,
MyDirective
],
<Page loaded="pageLoaded" actionBarHidden="true">
</Page>
<StackLayout verticalAlignment="middle">
<Button text="{{ abHidden ? 'Show ActionBar' : 'Hide ActionBar' }}" tap="onTap" textWrap="true" class="fa" />
</StackLayout>
在你的 .ts 文件中
export function onNavigatingTo(args: EventData) {
const page = <Page>args.object;
const vm = new Observable();
vm.set("abHidden", value);
page.bindingContext = vm;
}
ActionBar {
height: 0;
}
<ActionBar [title]="appTitle">
</ActionBar>
import { Component, OnInit } from "@angular/core";
import { Page } from "@nativescript/core";
@Component({
selector: "ns-items",
templateUrl: "./items.component.html",
})
export class ItemsComponent implements OnInit {
constructor(private page: Page) {
}
ngOnInit(): void {
this.page.actionBarHidden = true;
}
}