SVProgressHUD居中问题

最近碰到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];

    }

    

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值