Skip to content

Commit 2f9c7ec

Browse files
author
caven775
committed
ios 项目结构兼容
1 parent 05dbfe6 commit 2f9c7ec

File tree

6 files changed

+87
-6
lines changed

6 files changed

+87
-6
lines changed

example/ios/Runner/DStackTestCase.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ - (instancetype)init
2222
@"text": @"打开SecondViewController",
2323
@"clicked": ^(UIViewController *controller) {
2424
SecondViewController *secondVC = [[SecondViewController alloc] init];
25+
secondVC.hidesBottomBarWhenPushed = YES;
2526
[controller.navigationController pushViewController:secondVC animated:YES];
2627
}
2728
},

ios/Classes/Node/DActionManager.m

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ + (void)gesture:(DNode *)node willRemovedList:(NSArray<DNode *> *)nodeList
5959
[self sendMessageToFlutterWithFlutterNodes:nodeList
6060
node:popNode];
6161
}
62+
[self _checkTabBarWithNode:node popNodeList:nodeList];
6263
}
6364
}
6465

@@ -74,8 +75,8 @@ + (NSDictionary *)getPageTypeNodeList:(NSArray<DNode *> *)nodeList
7475
+ (void)enterPageWithNode:(DNode *)node
7576
{
7677
if (node.fromFlutter) {
77-
// 只处理来自Flutter消息通道的Node,并且是打开Native页面
7878
if (node.pageType == DNodePageTypeNative) {
79+
// 处理来自Flutter消息通道的Node,并且是打开Native页面
7980
// flutter打开naive页面
8081
DStackNode *stackNode = [self stackNodeFromNode:node];
8182
if (node.action == DNodeActionTypePush) {
@@ -87,6 +88,10 @@ + (void)enterPageWithNode:(DNode *)node
8788
[stack.delegate dStack:stack presentWithNode:stackNode];
8889
}];
8990
}
91+
} else if (node.pageType == DNodePageTypeFlutter) {
92+
// flutter打开flutter页面,检查tabBar是否隐藏
93+
[[NSNotificationCenter defaultCenter] postNotificationName:DStackNotificationNameChangeBottomBarVisible
94+
object:nil];
9095
}
9196
} else {
9297
// 来自Native的Node,并且是需要打开Flutter页面的,发消息至flutter,打开页面
@@ -152,6 +157,7 @@ + (void)closePageWithNode:(DNode *)node willRemovedList:(nullable NSArray<DNode
152157
} else if (currentNode.pageType == DNodePageTypeNative) {
153158
DStackLog(@"当前页面是Native,直接返回上一个页面,不需要处理");
154159
}
160+
[self _checkTabBarWithNode:node popNodeList:nodeList];
155161
}
156162

157163
/// 关闭一组页面
@@ -185,29 +191,55 @@ + (void)closePageListWithNode:(DNode *)node willRemovedList:(nullable NSArray<DN
185191
[self sendMessageToFlutterWithFlutterNodes:flutterNodes node:node];
186192
}
187193
if (!node.fromFlutter) { return;}
188-
189194
UINavigationController *navigation = [self currentNavigationControllerWithNode:node];
190195
if (node.action == DNodeActionTypePopToRoot ||
191196
node.action == DNodeActionTypePopToNativeRoot) {
192197
[navigation setValue:@(YES) forKey:@"dStackFlutterNodeMessage"];
193198
[navigation popToRootViewControllerAnimated:YES];
194199
[navigation setValue:@(NO) forKey:@"dStackFlutterNodeMessage"];
200+
[self _checkTabBarWithNode:node popNodeList:nodeList];
195201
return;
196202
}
197203

198204
NSInteger index = navigation.viewControllers.count - boundaryCount - nativeNodes.count - 1;
199205
index = index < 0 ? 0 : index;
200206
UIViewController *target = navigation.viewControllers[index];
201207
if (target) {
202-
if (target == navigation.topViewController) {return;}
208+
if (target == navigation.topViewController) {
209+
[self _checkTabBarWithNode:node popNodeList:nodeList];
210+
return;
211+
}
203212
[navigation setValue:@(YES) forKey:@"dStackFlutterNodeMessage"];
204213
[navigation popToViewController:target animated:NO];
205214
[navigation setValue:@(NO) forKey:@"dStackFlutterNodeMessage"];
215+
[self _checkTabBarWithNode:node popNodeList:nodeList];
206216
} else {
207217
DStackError(@"%@", @"没有找到需要关闭的controller");
208218
}
209219
}
210220

221+
/// 检查tabBar 的显示状态
222+
/// @param node node消息
223+
/// @param nodeList 节点列表
224+
+ (void)_checkTabBarWithNode:(DNode *)node popNodeList:(NSArray <DNode *>*)nodeList
225+
{
226+
DNode *preNode = nil;
227+
DNode *target = nodeList.lastObject;
228+
if (node.action == DNodeActionTypePopToRoot ||
229+
node.action == DNodeActionTypePopToNativeRoot) {
230+
target = nodeList.firstObject;
231+
}
232+
NSInteger index = [[DNodeManager sharedInstance].currentNodeList indexOfObject:target];
233+
if (index >= 1) {
234+
preNode = [[DNodeManager sharedInstance].currentNodeList objectAtIndex:index - 1];
235+
}
236+
if (preNode && preNode.isRootPage) {
237+
[[NSNotificationCenter defaultCenter] postNotificationName:DStackNotificationNameChangeBottomBarVisible
238+
object:nil
239+
userInfo:@{@"hidden": @(NO)}];
240+
}
241+
}
242+
211243
/// 发消息至Flutter
212244
/// @param flutterNodes 需要发送至flutter的节点信息
213245
/// @param node 目标节点信息

ios/Classes/Stack/DFlutterViewController.m

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ - (instancetype)init
1717
if(self = [super initWithEngine:[DStack sharedInstance].engine
1818
nibName:nil
1919
bundle:nil]) {
20-
self.view.backgroundColor = [UIColor whiteColor];
20+
[self config];
2121
}
2222
return self;
2323
}
@@ -27,11 +27,17 @@ - (instancetype)initWithCoder:(NSCoder *)coder
2727
if(self = [super initWithEngine:[DStack sharedInstance].engine
2828
nibName:nil
2929
bundle:nil]) {
30-
self.view.backgroundColor = [UIColor whiteColor];
30+
[self config];
3131
}
3232
return self;
3333
}
3434

35+
- (void)config
36+
{
37+
self.view.backgroundColor = [UIColor whiteColor];
38+
[self addNotification];
39+
}
40+
3541
- (void)viewWillAppear:(BOOL)animated
3642
{
3743
// 必须在页面显示之前判断engine是否存在FlutterViewController
@@ -70,4 +76,32 @@ - (void)_surfaceUpdated:(BOOL)appeared
7076
}
7177
}
7278

79+
- (void)addNotification
80+
{
81+
[[NSNotificationCenter defaultCenter] addObserver:self
82+
selector:@selector(changeBottomBarVisible:)
83+
name:DStackNotificationNameChangeBottomBarVisible
84+
object:nil];
85+
}
86+
87+
- (void)changeBottomBarVisible:(NSNotification *)notification
88+
{
89+
NSDictionary *userInfo = notification.userInfo;
90+
if (!userInfo) {
91+
if (self.tabBarController.tabBar.hidden == NO) {
92+
[self.tabBarController.tabBar setHidden:YES];
93+
}
94+
} else {
95+
BOOL hidden = [userInfo[@"hidden"] boolValue];
96+
if (self.tabBarController.tabBar.hidden) {
97+
[self.tabBarController.tabBar setHidden:hidden];
98+
}
99+
}
100+
}
101+
102+
- (void)dealloc
103+
{
104+
[[NSNotificationCenter defaultCenter] removeObserver:self];
105+
}
106+
73107
@end

ios/Classes/Stack/DNavigator.m

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,9 @@ - (void)d_stackPushViewController:(UIViewController *)controller animated:(BOOL)
594594
}
595595

596596
if ([controller isFlutterViewController]) {
597-
controller.hidesBottomBarWhenPushed = YES;
597+
if (self.dStackRootViewController != controller && self.viewControllers.count == 1) {
598+
controller.hidesBottomBarWhenPushed = YES;
599+
}
598600
}
599601

600602
if (!controller.isFlutterViewController && self.dStackRootViewController != controller) {

ios/Classes/Stack/DStack.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,5 +289,11 @@ applicationState:(DStackApplicationState)state
289289

290290
@end
291291

292+
293+
#pragma mark -DStackNotificationName
294+
295+
typedef NSString *DStackNotificationName;
296+
UIKIT_EXTERN DStackNotificationName const DStackNotificationNameChangeBottomBarVisible; // tabBar显示状态
297+
292298
NS_ASSUME_NONNULL_END
293299

ios/Classes/Stack/DStack.m

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,3 +543,9 @@ static void _dstack_engine_dyld_callback(const struct mach_header * mhp, intptr_
543543
__attribute__((constructor)) void _dstack_registerDyldCallback() {
544544
_dyld_register_func_for_add_image(_dstack_engine_dyld_callback);
545545
}
546+
547+
548+
549+
#pragma mark -DStackNotificationName
550+
551+
DStackNotificationName const DStackNotificationNameChangeBottomBarVisible = @"__DStackNotificationNameChangeBottomBarVisible";

0 commit comments

Comments
 (0)