forked from NativeScript/NativeScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtab-view.ios.ts
More file actions
262 lines (204 loc) · 9.86 KB
/
tab-view.ios.ts
File metadata and controls
262 lines (204 loc) · 9.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
import common = require("ui/tab-view/tab-view-common");
import definition = require("ui/tab-view");
import dependencyObservable = require("ui/core/dependency-observable");
import utilsModule = require("utils/utils");
import trace = require("trace");
import utils = require("utils/utils");
import view = require("ui/core/view");
import imageSource = require("image-source");
import types = require("utils/types");
// merge the exports of the common file with the exports of this file
declare var exports;
require("utils/module-merge").merge(common, exports);
class UITabBarControllerImpl extends UITabBarController {
static new(): UITabBarControllerImpl {
return <UITabBarControllerImpl>super.new();
}
private _owner: TabView;
public initWithOwner(owner: TabView): UITabBarControllerImpl {
this._owner = owner;
return this;
}
public viewDidLayoutSubviews(): void {
trace.write("TabView.UITabBarControllerClass.viewDidLayoutSubviews();", trace.categories.Debug);
super.viewDidLayoutSubviews();
if (this._owner.isLoaded) {
this._owner._updateLayout();
}
}
}
class UITabBarControllerDelegateImpl extends NSObject implements UITabBarControllerDelegate {
public static ObjCProtocols = [UITabBarControllerDelegate];
static new(): UITabBarControllerDelegateImpl {
return <UITabBarControllerDelegateImpl>super.new();
}
private _owner: TabView;
public initWithOwner(owner: TabView): UITabBarControllerDelegateImpl {
this._owner = owner;
return this;
}
public tabBarControllerDidSelectViewController(tabBarController: UITabBarController, viewController: UIViewController): void {
trace.write("TabView.UITabBarControllerDelegateClass.tabBarControllerDidSelectViewController(" + tabBarController + ", " + viewController + ");", trace.categories.Debug);
this._owner._onViewControllerShown(viewController);
}
}
class UINavigationControllerDelegateImpl extends NSObject implements UINavigationControllerDelegate {
public static ObjCProtocols = [UINavigationControllerDelegate];
static new(): UINavigationControllerDelegateImpl {
return <UINavigationControllerDelegateImpl>super.new();
}
private _owner: TabView;
public initWithOwner(owner: TabView): UINavigationControllerDelegateImpl {
this._owner = owner;
return this;
}
navigationControllerDidShowViewControllerAnimated(navigationController: UINavigationController, viewController: UIViewController, animated: boolean): void {
trace.write("TabView.UINavigationControllerDelegateClass.navigationControllerDidShowViewControllerAnimated(" + navigationController + ", " + viewController + ", " + animated + ");", trace.categories.Debug);
// We don't need Edit button in More screen.
navigationController.navigationBar.topItem.rightBarButtonItem = null;
this._owner._onViewControllerShown(viewController);
}
}
export class TabView extends common.TabView {
private _ios: UITabBarControllerImpl;
private _delegate: UITabBarControllerDelegateImpl;
private _moreNavigationControllerDelegate: UINavigationControllerDelegateImpl;
private _tabBarHeight: number = 0;
private _navBarHeight: number = 0;
private _iconsCache = {};
constructor() {
super();
this._ios = UITabBarControllerImpl.new().initWithOwner(this);
this._delegate = UITabBarControllerDelegateImpl.new().initWithOwner(this);
this._moreNavigationControllerDelegate = UINavigationControllerDelegateImpl.new().initWithOwner(this);
//This delegate is set on the last line of _addTabs method.
}
public onLoaded() {
super.onLoaded();
this._ios.delegate = this._delegate;
}
public onUnloaded() {
this._ios.delegate = null;
this._ios.moreNavigationController.delegate = null;
super.onUnloaded();
}
get ios(): UIViewController {
return this._ios;
}
get _nativeView(): UIView {
return this._ios.view;
}
public _onViewControllerShown(viewController: UIViewController) {
// This method could be called with the moreNavigationController or its list controller, so we have to check.
trace.write("TabView._onViewControllerShown(" + viewController + ");", trace.categories.Debug);
if (this._ios.viewControllers.containsObject(viewController)) {
this.selectedIndex = this._ios.viewControllers.indexOfObject(viewController);;
}
else {
trace.write("TabView._onViewControllerShown: viewController is not one of our viewControllers", trace.categories.Debug);
}
}
public _removeTabs(oldItems: Array<definition.TabViewItem>) {
trace.write("TabView._removeTabs(" + oldItems + ");", trace.categories.Debug);
super._removeTabs(oldItems);
var i: number;
var length = oldItems.length;
var oldItem: definition.TabViewItem;
for (i = 0; i < length; i++) {
oldItem = oldItems[i];
this._removeView(oldItem.view);
}
this._ios.viewControllers = null;
}
public _addTabs(newItems: Array<definition.TabViewItem>) {
trace.write("TabView._addTabs(" + newItems + ");", trace.categories.Debug);
super._addTabs(newItems);
var i: number;
var length = newItems.length;
var item: definition.TabViewItem;
var newControllers: NSMutableArray = NSMutableArray.alloc().initWithCapacity(length);
var newController: UIViewController;
for (i = 0; i < length; i++) {
item = newItems[i];
this._addView(item.view);
if (item.view.ios instanceof UIViewController) {
newController = <UIViewController>item.view.ios;
} else {
newController = new UIViewController();
newController.view.addSubview(item.view.ios);
}
var icon = this._getIcon(item.iconSource);
newController.tabBarItem = UITabBarItem.alloc().initWithTitleImageTag(item.title, icon, i);
if (!icon) {
newController.tabBarItem.setTitlePositionAdjustment({ horizontal: 0, vertical: -20 });
}
newControllers.addObject(newController);
}
this._ios.viewControllers = newControllers;
this._ios.customizableViewControllers = null;
// When we set this._ios.viewControllers, someone is clearing the moreNavigationController.delegate, so we have to reassign it each time here.
this._ios.moreNavigationController.delegate = this._moreNavigationControllerDelegate;
}
private _getIcon(iconSource: string): UIImage {
if (!iconSource) {
return null;
}
var image: UIImage;
image = this._iconsCache[iconSource];
if (!image) {
var is = imageSource.fromFileOrResource(iconSource);
if (is && is.ios) {
is.ios.renderingMode = UIImageRenderingMode.UIImageRenderingModeAlwaysOriginal;
this._iconsCache[iconSource] = is.ios;
image = is.ios;
}
}
return image;
}
public _onSelectedIndexPropertyChangedSetNativeValue(data: dependencyObservable.PropertyChangeData) {
super._onSelectedIndexPropertyChangedSetNativeValue(data);
var newIndex = data.newValue;
trace.write("TabView._onSelectedIndexPropertyChangedSetNativeValue(" + newIndex + ")", trace.categories.Debug);
if (types.isNullOrUndefined(newIndex)) {
return;
}
this._ios.selectedIndex = data.newValue;
// We will need to measure and arrange what became this._selectedView
this.requestLayout();
var args = { eventName: TabView.selectedIndexChangedEvent, object: this, oldIndex: data.oldValue, newIndex: data.newValue };
this.notify(args);
}
public onMeasure(widthMeasureSpec: number, heightMeasureSpec: number): void {
var nativeView = this._nativeView;
if (nativeView) {
var width = utils.layout.getMeasureSpecSize(widthMeasureSpec);
var widthMode = utils.layout.getMeasureSpecMode(widthMeasureSpec);
var height = utils.layout.getMeasureSpecSize(heightMeasureSpec);
var heightMode = utils.layout.getMeasureSpecMode(heightMeasureSpec);
this._tabBarHeight = utilsModule.ios.getActualHeight(this._ios.tabBar);
this._navBarHeight = utilsModule.ios.getActualHeight(this._ios.moreNavigationController.navigationBar);
var density = utils.layout.getDisplayDensity();
var measureWidth = 0;
var measureHeight = 0;
var child = this._selectedView;
if (child) {
var childHeightMeasureSpec = utils.layout.makeMeasureSpec(height - (this._navBarHeight + this._tabBarHeight), heightMode);
var childSize = view.View.measureChild(this, child, widthMeasureSpec, childHeightMeasureSpec);
measureHeight = childSize.measuredHeight;
measureWidth = childSize.measuredWidth;
}
measureWidth = Math.max(measureWidth, this.minWidth * density);
measureHeight = Math.max(measureHeight, this.minHeight * density);
var widthAndState = view.View.resolveSizeAndState(measureWidth, width, widthMode, 0);
var heightAndState = view.View.resolveSizeAndState(measureHeight, height, heightMode, 0);
this.setMeasuredDimension(widthAndState, heightAndState);
}
}
public onLayout(left: number, top: number, right: number, bottom: number): void {
super.onLayout(left, top, right, bottom);
var child = this._selectedView;
if (child) {
view.View.layoutChild(this, child, 0, this._navBarHeight, right, (bottom - this._navBarHeight - this._tabBarHeight));
}
}
}