本文分享在 Ionic 裡隱藏 tabs 的做法,供需要的朋友參考。
1. 先在 ion-tabs 上掛一個由 $rootScope 控制的 class:
<ion-tabs ng-class="{'tabs-item-hide': $root.hideTabs}"> <!-- tabs --> </ion-tabs>
2. 在該控制器下加上 directive:
var module = angular.module('app.directives', []); module.directive('showTabs', function ($rootScope) { return { restrict: 'A', link: function ($scope, $el) { $rootScope.hideTabs = false; } }; }).directive('hideTabs', function ($rootScope) { return { restrict: 'A', link: function ($scope, $el) { $rootScope.hideTabs = true; } }; })
3. 在需要隱藏的 html 頁面加上 hide-tabs:
<ion-view title="New Entry Form" hide-tabs> <!-- view content --> </ion-tabs>
4. 當頁面返回主頁時要讓 tabs 重新出現,需在該控制器補上一段(這主要是解決 Android 上 tabs 仍然隱藏不見的問題):
$scope.$on('$ionicView.enter', function () { // 顯示 tabs $rootScope.hideTabs = false; });
5. 如果你用的是 tabs-top,還會遇到另一個問題:<ion-content> 的內容有一部分被遮住。解法是改寫 directive,不再使用 showTabs:
.directive('hideTabs', function ($rootScope) { return { restrict: 'A', link: function (scope, element, attributes) { scope.$on('$ionicView.beforeEnter', function () { scope.$watch(attributes.hideTabs, function (value) { $rootScope.hideTabs = value; }); }); scope.$on('$ionicView.beforeLeave', function () { $rootScope.hideTabs = false; }); } }; })
歸納一下:tabs 放在底部的話,前面幾個步驟基本不會出問題;一旦放到頂部就會牽扯到 content 的高度計算,容易出狀況。
另外也值得考慮用 <ion-slide> 取代 <ion-tabs>——無論是頁面之間的切換還是 slide 內部的滑動體驗都會明顯提升,在 Android 上尤其有感。