diff --git a/src/cdk/overlay/position/flexible-connected-position-strategy.ts b/src/cdk/overlay/position/flexible-connected-position-strategy.ts index 1910f655e652..50eb45128f3a 100644 --- a/src/cdk/overlay/position/flexible-connected-position-strategy.ts +++ b/src/cdk/overlay/position/flexible-connected-position-strategy.ts @@ -468,13 +468,28 @@ export class FlexibleConnectedPositionStrategy implements PositionStrategy { overlayStartY = pos.overlayY == 'top' ? 0 : -overlayRect.height; } + // On iOS, the .cdk-overlay-container will be positioned absolute instead of fixed. + // Adjust the y coordinate in that case by the scroll amount. + const overlayScrollY = this._getOverlayScrollY(); + // The (x, y) coordinates of the overlay. return { x: originPoint.x + overlayStartX, - y: originPoint.y + overlayStartY, + y: originPoint.y + overlayScrollY + overlayStartY, }; } + /** + * Gets the y coordinates of the overlay host element. On iOS when the keyboard is visible, + * .cdk-overlay-container will have an absolute position instead of fixed. When that happens, + * this will return a non-zero value. + */ + private _getOverlayScrollY(): number { + const clientRect = this._overlayRef.hostElement.getBoundingClientRect(); + + return Math.abs(clientRect.top); + } + /** Gets how well an overlay at the given point will fit within the viewport. */ private _getOverlayFit(point: Point, overlay: ClientRect, viewport: ClientRect, position: ConnectedPosition): OverlayFit {