Skip to content

Commit b489e52

Browse files
Hristo HristovHristo Hristov
authored andcommitted
Fix iOS 11 titleView misplacement in ActionBar
1 parent 2e2467c commit b489e52

File tree

1 file changed

+31
-18
lines changed

1 file changed

+31
-18
lines changed

tns-core-modules/ui/action-bar/action-bar.ios.ts

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
import { IOSActionItemSettings, ActionItem as ActionItemDefinition } from ".";
22
import { ActionItemBase, ActionBarBase, isVisible, View, colorProperty, backgroundColorProperty, backgroundInternalProperty, flatProperty, layout, Color } from "./action-bar-common";
33
import { ImageSource, fromFileOrResource } from "../../image-source";
4+
import { ios as iosUtils } from "../../utils/utils";
45

56
export * from "./action-bar-common";
67

8+
const majorVersion = iosUtils.MajorVersion;
9+
const UNSPECIFIED = layout.makeMeasureSpec(0, layout.UNSPECIFIED);
10+
711
class TapBarItemHandlerImpl extends NSObject {
812
private _owner: WeakRef<ActionItemDefinition>;
913

@@ -46,12 +50,12 @@ export class NavigationButton extends ActionItem {
4650
export class ActionBar extends ActionBarBase {
4751

4852
get ios(): UIView {
49-
let page = this.page;
53+
const page = this.page;
5054
if (!page || !page.parent) {
5155
return;
5256
}
5357

54-
let viewController = (<UIViewController>page.ios);
58+
const viewController = (<UIViewController>page.ios);
5559
if (viewController.navigationController !== null) {
5660
return viewController.navigationController.navigationBar;
5761
}
@@ -102,10 +106,6 @@ export class ActionBar extends ActionBarBase {
102106
return;
103107
}
104108

105-
if (!this.isLayoutValid) {
106-
this.layoutInternal();
107-
}
108-
109109
const viewController = (<UIViewController>page.ios);
110110
const navigationItem: UINavigationItem = viewController.navigationItem;
111111
const navController = <UINavigationController>page.frame.ios.controller;
@@ -169,6 +169,10 @@ export class ActionBar extends ActionBarBase {
169169

170170
// the 'flat' property may have changed in between pages
171171
this.updateFlatness(navigationBar);
172+
173+
if (!this.isLayoutValid) {
174+
this.layoutInternal();
175+
}
172176
}
173177

174178
private populateMenuItems(navigationItem: UINavigationItem) {
@@ -275,16 +279,13 @@ export class ActionBar extends ActionBarBase {
275279
const heightMode = layout.getMeasureSpecMode(heightMeasureSpec);
276280

277281
if (this.titleView) {
278-
View.measureChild(this, this.titleView,
279-
layout.makeMeasureSpec(width, layout.AT_MOST),
280-
layout.makeMeasureSpec(height, layout.AT_MOST));
282+
View.measureChild(this, this.titleView, UNSPECIFIED, UNSPECIFIED);
281283
}
282284

283285
this.actionItems.getItems().forEach((actionItem) => {
284-
if (actionItem.actionView) {
285-
View.measureChild(this, actionItem.actionView,
286-
layout.makeMeasureSpec(width, layout.AT_MOST),
287-
layout.makeMeasureSpec(height, layout.AT_MOST));
286+
const actionView = actionItem.actionView;
287+
if (actionView) {
288+
View.measureChild(this, actionView, UNSPECIFIED, UNSPECIFIED);
288289
}
289290
});
290291

@@ -293,12 +294,24 @@ export class ActionBar extends ActionBarBase {
293294
}
294295

295296
public onLayout(left: number, top: number, right: number, bottom: number) {
296-
View.layoutChild(this, this.titleView, 0, 0, right - left, bottom - top);
297+
const titleView = this.titleView;
298+
if (titleView) {
299+
if (majorVersion > 10) {
300+
// On iOS 11 titleView is wrapped in another view that is centered with constraints.
301+
View.layoutChild(this, titleView, 0, 0, titleView.getMeasuredWidth(), titleView.getMeasuredHeight());
302+
} else {
303+
// On iOS <11 titleView is direct child of UINavigationBar so we give it full width and leave
304+
// the layout to center it.
305+
View.layoutChild(this, titleView, 0, 0, right - left, bottom - top);
306+
}
307+
}
308+
297309
this.actionItems.getItems().forEach((actionItem) => {
298-
if (actionItem.actionView && actionItem.actionView.ios) {
299-
let measuredWidth = actionItem.actionView.getMeasuredWidth();
300-
let measuredHeight = actionItem.actionView.getMeasuredHeight();
301-
View.layoutChild(this, actionItem.actionView, 0, 0, measuredWidth, measuredHeight);
310+
const actionView = actionItem.actionView;
311+
if (actionView && actionView.ios) {
312+
const measuredWidth = actionView.getMeasuredWidth();
313+
const measuredHeight = actionView.getMeasuredHeight();
314+
View.layoutChild(this, actionView, 0, 0, measuredWidth, measuredHeight);
302315
}
303316
});
304317

0 commit comments

Comments
 (0)