From 15bc6bd60f5fced2a9870ad8693b99d34c954b2e Mon Sep 17 00:00:00 2001 From: WenJingRui <2539699336@qq.com> Date: Fri, 1 Apr 2022 03:30:04 +0800 Subject: [PATCH] [iOS] Ensure the keyboard bottom inset is zero when keyboard dismiss. (flutter/engine#31982) --- .../framework/Source/FlutterViewController.mm | 26 +++++++++++++++-- .../Source/FlutterViewControllerTest.mm | 28 +++++++++++++++++++ 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm index 25d94fddc2..6bd478f39d 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm @@ -1180,8 +1180,30 @@ static flutter::PointerData::DeviceKind DeviceKindFromTouchType(UITouch* touch) } - (void)keyboardWillBeHidden:(NSNotification*)notification { - // When keyboard hide, the keyboardWillChangeFrame function will be called to update viewport - // metrics. So do not call [self updateViewportMetrics] here again. + NSDictionary* info = [notification userInfo]; + + if (@available(iOS 9, *)) { + // Ignore keyboard notifications related to other apps. + id isLocal = info[UIKeyboardIsLocalUserInfoKey]; + if (isLocal && ![isLocal boolValue]) { + return; + } + } + + // Ignore keyboard notifications if engine’s viewController is not current viewController. + if ([_engine.get() viewController] != self) { + return; + } + + if (self.targetViewInsetBottom != 0) { + // Ensure the keyboard will be dismissed. Just like the keyboardWillChangeFrame, + // keyboardWillBeHidden is also in an animation block in iOS sdk, so we don't need to set the + // animation curve. Related issue: https://github.com/flutter/flutter/issues/99951 + self.targetViewInsetBottom = 0; + NSTimeInterval duration = + [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue]; + [self startKeyBoardAnimation:duration]; + } } - (void)startKeyBoardAnimation:(NSTimeInterval)duration { diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewControllerTest.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewControllerTest.mm index 0ce51bdd9d..24d194fa86 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewControllerTest.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewControllerTest.mm @@ -125,6 +125,9 @@ typedef enum UIAccessibilityContrast : NSInteger { @end @interface FlutterViewController (Tests) + +@property(nonatomic, assign) double targetViewInsetBottom; + - (void)surfaceUpdated:(BOOL)appeared; - (void)performOrientationUpdate:(UIInterfaceOrientationMask)new_preferences; - (void)handlePressEvent:(FlutterUIPressProxy*)press @@ -135,6 +138,7 @@ typedef enum UIAccessibilityContrast : NSInteger { - (void)applicationWillTerminate:(NSNotification*)notification; - (void)goToApplicationLifecycle:(nonnull NSString*)state; - (void)keyboardWillChangeFrame:(NSNotification*)notification; +- (void)keyboardWillBeHidden:(NSNotification*)notification; - (void)startKeyBoardAnimation:(NSTimeInterval)duration; - (void)ensureViewportMetricsIsCorrect; - (void)invalidateDisplayLink; @@ -198,6 +202,30 @@ typedef enum UIAccessibilityContrast : NSInteger { [self waitForExpectationsWithTimeout:5.0 handler:nil]; } +- (void)testEnsureBottomInsetIsZeroWhenKeyboardDismissed { + FlutterEngine* mockEngine = OCMPartialMock([[FlutterEngine alloc] init]); + [mockEngine createShell:@"" libraryURI:@"" initialRoute:nil]; + FlutterViewController* viewController = [[FlutterViewController alloc] initWithEngine:mockEngine + nibName:nil + bundle:nil]; + + FlutterViewController* viewControllerMock = OCMPartialMock(viewController); + CGRect keyboardFrame = CGRectMake(0, 0, 0, 0); + BOOL isLocal = YES; + NSNotification* fakeNotification = [NSNotification + notificationWithName:@"" + object:nil + userInfo:@{ + @"UIKeyboardFrameEndUserInfoKey" : [NSValue valueWithCGRect:keyboardFrame], + @"UIKeyboardAnimationDurationUserInfoKey" : @(0.25), + @"UIKeyboardIsLocalUserInfoKey" : @(isLocal) + }]; + + viewControllerMock.targetViewInsetBottom = 10; + [viewControllerMock keyboardWillBeHidden:fakeNotification]; + XCTAssertTrue(viewControllerMock.targetViewInsetBottom == 0); +} + - (void)testEnsureViewportMetricsWillInvokeAndDisplayLinkWillInvalidateInViewDidDisappear { FlutterEngine* mockEngine = OCMPartialMock([[FlutterEngine alloc] init]); [mockEngine createShell:@"" libraryURI:@"" initialRoute:nil];