最近碰到SVProgressHUD由于键盘而导致不居中的问题,下面列出解决方案:
在SVProgressHUD.m源代码中找到positionHUD方法加一行代码keyboardHeight = 0;
- (void)positionHUD:(NSNotification*)notification {
CGFloat keyboardHeight = 0.0f;
double animationDuration = 0.0;
self.frame = UIScreen.mainScreen.bounds;
#if !defined(SV_APP_EXTENSIONS)
UIInterfaceOrientation orientation = UIApplication.sharedApplication.statusBarOrientation;
#else
UIInterfaceOrientation orientation = CGRectGetWidth(self.frame) > CGRectGetHeight(self.frame) ? UIInterfaceOrientationLandscapeLeft : UIInterfaceOrientationPortrait;
#endif
// no transforms applied to window in iOS 8, but only if compiled with iOS 8 sdk as base sdk, otherwise system supports old rotation logic.
BOOL ignoreOrientation = NO;
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000
if ([[NSProcessInfo processInfo] respondsToSelector:@selector(operatingSystemVersion)]) {
ignoreOrientation = YES;
}
#endif
if(notification) {
NSDictionary* keyboardInfo = [notification userInfo];
CGRect keyboardFrame = [[keyboardInfo valueForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
animationDuration = [[keyboardInfo valueForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
if(notification.name == UIKeyboardWillShowNotification || notification.name == UIKeyboardDidShowNotification) {
if(ignoreOrientation || UIInterfaceOrientationIsPortrait(orientation))
keyboardHeight = CGRectGetHeight(keyboardFrame);
else
keyboardHeight = CGRectGetWidth(keyboardFrame);
}
} else {
keyboardHeight = self.visibleKeyboardHeight;
}
//加的代码,不考虑键盘高度
keyboardHeight = 0;
CGRect orientationFrame = self.bounds;
#if !defined(SV_APP_EXTENSIONS)
CGRect statusBarFrame = UIApplication.sharedApplication.statusBarFrame;
#else
CGRect statusBarFrame = CGRectZero;
#endif
if(!ignoreOrientation && UIInterfaceOrientationIsLandscape(orientation)) {
float temp = CGRectGetWidth(orientationFrame);
orientationFrame.size.width = CGRectGetHeight(orientationFrame);
orientationFrame.size.height = temp;
temp = CGRectGetWidth(statusBarFrame);
statusBarFrame.size.width = CGRectGetHeight(statusBarFrame);
statusBarFrame.size.height = temp;
}
CGFloat activeHeight = CGRectGetHeight(orientationFrame);
if(keyboardHeight > 0)
activeHeight += CGRectGetHeight(statusBarFrame)*2;
activeHeight -= keyboardHeight;
CGFloat posY = floor(activeHeight*0.45);
CGFloat posX = CGRectGetWidth(orientationFrame)/2;
CGPoint newCenter;
CGFloat rotateAngle;
if (ignoreOrientation) {
rotateAngle = 0.0;
newCenter = CGPointMake(posX, posY);
} else {
switch (orientation) {
case UIInterfaceOrientationPortraitUpsideDown:
rotateAngle = M_PI;
newCenter = CGPointMake(posX, CGRectGetHeight(orientationFrame)-posY);
break;
case UIInterfaceOrientationLandscapeLeft:
rotateAngle = -M_PI/2.0f;
newCenter = CGPointMake(posY, posX);
break;
case UIInterfaceOrientationLandscapeRight:
rotateAngle = M_PI/2.0f;
newCenter = CGPointMake(CGRectGetHeight(orientationFrame)-posY, posX);
break;
default: // as UIInterfaceOrientationPortrait
rotateAngle = 0.0;
newCenter = CGPointMake(posX, posY);
break;
}
}
if(notification) {
[UIView animateWithDuration:animationDuration
delay:0
options:UIViewAnimationOptionAllowUserInteraction
animations:^{
[self moveToPoint:newCenter rotateAngle:rotateAngle];
[self.hudView setNeedsDisplay];
} completion:NULL];
} else {
[self moveToPoint:newCenter rotateAngle:rotateAngle];
[self.hudView setNeedsDisplay];
}
}