fix(ios): Objective-C class name "UITabBar..." already in use handling

This commit is contained in:
Nathan Walker
2020-09-07 10:50:24 -07:00
parent 5e4cd9865a
commit c8273931f3
3 changed files with 157 additions and 143 deletions

View File

@ -3,7 +3,7 @@
"main": "index", "main": "index",
"types": "index.d.ts", "types": "index.d.ts",
"description": "NativeScript Core Modules", "description": "NativeScript Core Modules",
"version": "6.5.17", "version": "6.5.18",
"homepage": "https://www.nativescript.org", "homepage": "https://www.nativescript.org",
"repository": { "repository": {
"type": "git", "type": "git",

View File

@ -18,159 +18,173 @@ export * from "./tab-view-common";
const majorVersion = iosUtils.MajorVersion; const majorVersion = iosUtils.MajorVersion;
const isPhone = device.deviceType === "Phone"; const isPhone = device.deviceType === "Phone";
class UITabBarControllerImpl extends UITabBarController { let UITabBarControllerImpl;
let UITabBarControllerDelegateImpl;
let UINavigationControllerDelegateImpl;
private _owner: WeakRef<TabView>; const setupControllers = function () {
if (typeof UITabBarControllerImpl === 'undefined') {
public static initWithOwner(owner: WeakRef<TabView>): UITabBarControllerImpl { class UITabBarControllerClass extends UITabBarController {
let handler = <UITabBarControllerImpl>UITabBarControllerImpl.new();
handler._owner = owner; private _owner: WeakRef<TabView>;
return handler; public static initWithOwner(owner: WeakRef<TabView>): typeof UITabBarControllerImpl {
} let handler = <typeof UITabBarControllerImpl>UITabBarControllerImpl.new();
handler._owner = owner;
public viewDidLoad(): void {
super.viewDidLoad(); return handler;
// Unify translucent and opaque bars layout
// this.edgesForExtendedLayout = UIRectEdgeBottom;
this.extendedLayoutIncludesOpaqueBars = true;
}
@profile
public viewWillAppear(animated: boolean): void {
super.viewWillAppear(animated);
const owner = this._owner.get();
if (!owner) {
return;
} }
iosView.updateAutoAdjustScrollInsets(this, owner); public viewDidLoad(): void {
super.viewDidLoad();
if (!owner.parent) {
owner.callLoaded(); // Unify translucent and opaque bars layout
// this.edgesForExtendedLayout = UIRectEdgeBottom;
this.extendedLayoutIncludesOpaqueBars = true;
} }
}
@profile
@profile public viewWillAppear(animated: boolean): void {
public viewDidDisappear(animated: boolean): void { super.viewWillAppear(animated);
super.viewDidDisappear(animated);
const owner = this._owner.get();
if (owner && !owner.parent && owner.isLoaded && !this.presentedViewController) {
owner.callUnloaded();
}
}
public viewWillTransitionToSizeWithTransitionCoordinator(size: CGSize, coordinator: UIViewControllerTransitionCoordinator): void {
super.viewWillTransitionToSizeWithTransitionCoordinator(size, coordinator);
coordinator.animateAlongsideTransitionCompletion(null, () => {
const owner = this._owner.get(); const owner = this._owner.get();
if (owner && owner.items) { if (!owner) {
owner.items.forEach(tabItem => tabItem._updateTitleAndIconPositions()); return;
} }
});
} iosView.updateAutoAdjustScrollInsets(this, owner);
// Mind implementation for other controllers if (!owner.parent) {
public traitCollectionDidChange(previousTraitCollection: UITraitCollection): void { owner.callLoaded();
super.traitCollectionDidChange(previousTraitCollection); }
}
if (majorVersion >= 13) {
@profile
public viewDidDisappear(animated: boolean): void {
super.viewDidDisappear(animated);
const owner = this._owner.get(); const owner = this._owner.get();
if (owner && if (owner && !owner.parent && owner.isLoaded && !this.presentedViewController) {
this.traitCollection.hasDifferentColorAppearanceComparedToTraitCollection && owner.callUnloaded();
this.traitCollection.hasDifferentColorAppearanceComparedToTraitCollection(previousTraitCollection)) { }
owner.notify({ eventName: iosView.traitCollectionColorAppearanceChangedEvent, object: owner }); }
public viewWillTransitionToSizeWithTransitionCoordinator(size: CGSize, coordinator: UIViewControllerTransitionCoordinator): void {
super.viewWillTransitionToSizeWithTransitionCoordinator(size, coordinator);
coordinator.animateAlongsideTransitionCompletion(null, () => {
const owner = this._owner.get();
if (owner && owner.items) {
owner.items.forEach(tabItem => tabItem._updateTitleAndIconPositions());
}
});
}
// Mind implementation for other controllers
public traitCollectionDidChange(previousTraitCollection: UITraitCollection): void {
super.traitCollectionDidChange(previousTraitCollection);
if (majorVersion >= 13) {
const owner = this._owner.get();
if (owner &&
this.traitCollection.hasDifferentColorAppearanceComparedToTraitCollection &&
this.traitCollection.hasDifferentColorAppearanceComparedToTraitCollection(previousTraitCollection)) {
owner.notify({ eventName: iosView.traitCollectionColorAppearanceChangedEvent, object: owner });
}
} }
} }
} }
} UITabBarControllerImpl = UITabBarControllerClass;
class UITabBarControllerDelegateImpl extends NSObject implements UITabBarControllerDelegate { class UITabBarControllerDelegateClass extends NSObject implements UITabBarControllerDelegate {
public static ObjCProtocols = [UITabBarControllerDelegate]; public static ObjCProtocols = [UITabBarControllerDelegate];
private _owner: WeakRef<TabView>; private _owner: WeakRef<TabView>;
public static initWithOwner(owner: WeakRef<TabView>): UITabBarControllerDelegateImpl { public static initWithOwner(owner: WeakRef<TabView>): typeof UITabBarControllerDelegateImpl {
let delegate = <UITabBarControllerDelegateImpl>UITabBarControllerDelegateImpl.new(); let delegate = <typeof UITabBarControllerDelegateImpl>UITabBarControllerDelegateImpl.new();
delegate._owner = owner; delegate._owner = owner;
return delegate; return delegate;
}
public tabBarControllerShouldSelectViewController(tabBarController: UITabBarController, viewController: UIViewController): boolean {
if (traceEnabled()) {
traceWrite("TabView.delegate.SHOULD_select(" + tabBarController + ", " + viewController + ");", traceCategories.Debug);
} }
let owner = this._owner.get(); public tabBarControllerShouldSelectViewController(tabBarController: UITabBarController, viewController: UIViewController): boolean {
if (owner) { if (traceEnabled()) {
// "< More" cannot be visible after clicking on the main tab bar buttons. traceWrite("TabView.delegate.SHOULD_select(" + tabBarController + ", " + viewController + ");", traceCategories.Debug);
let backToMoreWillBeVisible = false; }
owner._handleTwoNavigationBars(backToMoreWillBeVisible);
let owner = this._owner.get();
if (owner) {
// "< More" cannot be visible after clicking on the main tab bar buttons.
let backToMoreWillBeVisible = false;
owner._handleTwoNavigationBars(backToMoreWillBeVisible);
}
if ((<any>tabBarController).selectedViewController === viewController) {
return false;
}
(<any>tabBarController)._willSelectViewController = viewController;
return true;
} }
if ((<any>tabBarController).selectedViewController === viewController) { public tabBarControllerDidSelectViewController(tabBarController: UITabBarController, viewController: UIViewController): void {
return false; if (traceEnabled()) {
} traceWrite("TabView.delegate.DID_select(" + tabBarController + ", " + viewController + ");", traceCategories.Debug);
}
(<any>tabBarController)._willSelectViewController = viewController;
const owner = this._owner.get();
return true; if (owner) {
} owner._onViewControllerShown(viewController);
}
public tabBarControllerDidSelectViewController(tabBarController: UITabBarController, viewController: UIViewController): void {
if (traceEnabled()) { (<any>tabBarController)._willSelectViewController = undefined;
traceWrite("TabView.delegate.DID_select(" + tabBarController + ", " + viewController + ");", traceCategories.Debug);
}
const owner = this._owner.get();
if (owner) {
owner._onViewControllerShown(viewController);
}
(<any>tabBarController)._willSelectViewController = undefined;
}
}
class UINavigationControllerDelegateImpl extends NSObject implements UINavigationControllerDelegate {
public static ObjCProtocols = [UINavigationControllerDelegate];
private _owner: WeakRef<TabView>;
public static initWithOwner(owner: WeakRef<TabView>): UINavigationControllerDelegateImpl {
let delegate = <UINavigationControllerDelegateImpl>UINavigationControllerDelegateImpl.new();
delegate._owner = owner;
return delegate;
}
navigationControllerWillShowViewControllerAnimated(navigationController: UINavigationController, viewController: UIViewController, animated: boolean): void {
if (traceEnabled()) {
traceWrite("TabView.moreNavigationController.WILL_show(" + navigationController + ", " + viewController + ", " + animated + ");", traceCategories.Debug);
}
let owner = this._owner.get();
if (owner) {
// If viewController is one of our tab item controllers, then "< More" will be visible shortly.
// Otherwise viewController is the UIMoreListController which shows the list of all tabs beyond the 4th tab.
let backToMoreWillBeVisible = owner._ios.viewControllers.containsObject(viewController);
owner._handleTwoNavigationBars(backToMoreWillBeVisible);
} }
} }
UITabBarControllerDelegateImpl = UITabBarControllerDelegateClass;
navigationControllerDidShowViewControllerAnimated(navigationController: UINavigationController, viewController: UIViewController, animated: boolean): void { class UINavigationControllerDelegateClass extends NSObject implements UINavigationControllerDelegate {
if (traceEnabled()) { public static ObjCProtocols = [UINavigationControllerDelegate];
traceWrite("TabView.moreNavigationController.DID_show(" + navigationController + ", " + viewController + ", " + animated + ");", traceCategories.Debug);
private _owner: WeakRef<TabView>;
public static initWithOwner(owner: WeakRef<TabView>): typeof UINavigationControllerDelegateImpl {
let delegate = <typeof UINavigationControllerDelegateImpl>UINavigationControllerDelegateImpl.new();
delegate._owner = owner;
return delegate;
} }
// We don't need Edit button in More screen.
navigationController.navigationBar.topItem.rightBarButtonItem = null; navigationControllerWillShowViewControllerAnimated(navigationController: UINavigationController, viewController: UIViewController, animated: boolean): void {
let owner = this._owner.get(); if (traceEnabled()) {
if (owner) { traceWrite("TabView.moreNavigationController.WILL_show(" + navigationController + ", " + viewController + ", " + animated + ");", traceCategories.Debug);
owner._onViewControllerShown(viewController); }
let owner = this._owner.get();
if (owner) {
// If viewController is one of our tab item controllers, then "< More" will be visible shortly.
// Otherwise viewController is the UIMoreListController which shows the list of all tabs beyond the 4th tab.
let backToMoreWillBeVisible = owner._ios.viewControllers.containsObject(viewController);
owner._handleTwoNavigationBars(backToMoreWillBeVisible);
}
}
navigationControllerDidShowViewControllerAnimated(navigationController: UINavigationController, viewController: UIViewController, animated: boolean): void {
if (traceEnabled()) {
traceWrite("TabView.moreNavigationController.DID_show(" + navigationController + ", " + viewController + ", " + animated + ");", traceCategories.Debug);
}
// We don't need Edit button in More screen.
navigationController.navigationBar.topItem.rightBarButtonItem = null;
let owner = this._owner.get();
if (owner) {
owner._onViewControllerShown(viewController);
}
} }
} }
} UINavigationControllerDelegateImpl = UINavigationControllerDelegateClass;
}
};
setupControllers();
function updateTitleAndIconPositions(tabItem: TabViewItem, tabBarItem: UITabBarItem, controller: UIViewController) { function updateTitleAndIconPositions(tabItem: TabViewItem, tabBarItem: UITabBarItem, controller: UIViewController) {
if (!tabItem || !tabBarItem) { if (!tabItem || !tabBarItem) {
@ -256,11 +270,11 @@ export class TabViewItem extends TabViewItemBase {
} }
export class TabView extends TabViewBase { export class TabView extends TabViewBase {
public viewController: UITabBarControllerImpl; public viewController: typeof UITabBarControllerImpl;
public items: TabViewItem[]; public items: TabViewItem[];
public _ios: UITabBarControllerImpl; public _ios: typeof UITabBarControllerImpl;
private _delegate: UITabBarControllerDelegateImpl; private _delegate: typeof UITabBarControllerDelegateImpl;
private _moreNavigationControllerDelegate: UINavigationControllerDelegateImpl; private _moreNavigationControllerDelegate: typeof UINavigationControllerDelegateImpl;
private _iconsCache = {}; private _iconsCache = {};
constructor() { constructor() {

View File

@ -3,7 +3,7 @@
"main": "index", "main": "index",
"types": "index.d.ts", "types": "index.d.ts",
"description": "NativeScript Core Modules", "description": "NativeScript Core Modules",
"version": "6.5.17", "version": "6.5.18",
"homepage": "https://www.nativescript.org", "homepage": "https://www.nativescript.org",
"repository": { "repository": {
"type": "git", "type": "git",
@ -11,7 +11,7 @@
}, },
"license": "Apache-2.0", "license": "Apache-2.0",
"dependencies": { "dependencies": {
"@nativescript/core": "6.5.17" "@nativescript/core": "6.5.18"
}, },
"devDependencies": { "devDependencies": {
"tns-platform-declarations": "6.5.15" "tns-platform-declarations": "6.5.15"