From fd53efb543cc83e81212cb8651f0d6f2d9f276f2 Mon Sep 17 00:00:00 2001 From: Kevin Date: Tue, 22 Nov 2016 12:29:48 +0800 Subject: [PATCH] Translate origin of container view to be vertical-centered if there is enough space. --- STPopup/STPopupController.m | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/STPopup/STPopupController.m b/STPopup/STPopupController.m index f0eeefd..e091c3e 100644 --- a/STPopup/STPopupController.m +++ b/STPopup/STPopupController.m @@ -695,19 +695,22 @@ - (void)adjustContainerViewOrigin offsetY = keyboardHeight; } else { - CGFloat spacing = 5; - offsetY = _containerView.frame.origin.y + _containerView.bounds.size.height - (_containerViewController.view.bounds.size.height - keyboardHeight - spacing); - if (offsetY <= 0) { // _containerView can be totally shown, so no need to reposition - return; - } - CGFloat statusBarHeight = [UIApplication sharedApplication].statusBarFrame.size.height; - - if (_containerView.frame.origin.y - offsetY < statusBarHeight) { // _containerView will be covered by status bar if it is repositioned with "offsetY" - offsetY = _containerView.frame.origin.y - statusBarHeight; - // currentTextField can not be totally shown if _containerView is going to repositioned with "offsetY" - if (textFieldBottomY - offsetY > _containerViewController.view.bounds.size.height - keyboardHeight - spacing) { - offsetY = textFieldBottomY - (_containerViewController.view.bounds.size.height - keyboardHeight - spacing); + if (_containerView.bounds.size.height <= _containerViewController.view.bounds.size.height - keyboardHeight - statusBarHeight) { + offsetY = _containerView.frame.origin.y - (statusBarHeight + (_containerViewController.view.bounds.size.height - keyboardHeight - statusBarHeight - _containerView.bounds.size.height) / 2); + } + else { + CGFloat spacing = 5; + offsetY = _containerView.frame.origin.y + _containerView.bounds.size.height - (_containerViewController.view.bounds.size.height - keyboardHeight - spacing); + if (offsetY <= 0) { // _containerView can be totally shown, so no need to translate the origin + return; + } + if (_containerView.frame.origin.y - offsetY < statusBarHeight) { // _containerView will be covered by status bar if the origin is translated by "offsetY" + offsetY = _containerView.frame.origin.y - statusBarHeight; + // currentTextField can not be totally shown if _containerView is going to repositioned with "offsetY" + if (textFieldBottomY - offsetY > _containerViewController.view.bounds.size.height - keyboardHeight - spacing) { + offsetY = textFieldBottomY - (_containerViewController.view.bounds.size.height - keyboardHeight - spacing); + } } } }