@@ -344,6 +344,7 @@ - (instancetype)initWithFrame:(CGRect)frame {
344344 _defaultMaskType = SVProgressHUDMaskTypeNone;
345345 _defaultStyle = SVProgressHUDStyleLight;
346346 _defaultAnimationType = SVProgressHUDAnimationTypeFlat;
347+ _minimumSize = CGSizeMake (100 .0f , 100 .0f );
347348
348349 if ([UIFont respondsToSelector: @selector (preferredFontForTextStyle: )]) {
349350 _font = [UIFont preferredFontForTextStyle: UIFontTextStyleSubheadline];
@@ -395,12 +396,14 @@ - (instancetype)initWithFrame:(CGRect)frame {
395396- (void )updateHUDFrame {
396397 // For the beginning use default values, these
397398 // might get update if string is too large etc.
398- CGFloat hudWidth = 100 .0f ;
399- CGFloat hudHeight = 100 .0f ;
400- CGFloat stringHeightBuffer = 20 .0f ;
401- CGFloat stringAndContentHeightBuffer = 80 .0f ;
399+ CGFloat hudWidth;
400+ CGFloat hudHeight;
402401 CGRect labelRect = CGRectZero;
403402
403+ CGFloat verticalSpacing = 12 .0f ; // |-12-content-(8-label-)12-|
404+ CGFloat horizontalSpacing = 12 .0f ; // |-12-content-12-|
405+ CGFloat progressLabelSpacing = 8 .0f ; // content-8-label; progess = spinner or image
406+
404407 // Check if an image or progress ring is displayed
405408 BOOL imageUsed = (self.imageView .image ) && !(self.imageView .hidden );
406409 BOOL progressUsed = self.imageView .hidden ;
@@ -409,12 +412,11 @@ - (void)updateHUDFrame {
409412 NSString *string = self.statusLabel .text ;
410413 if (string) {
411414 CGSize constraintSize = CGSizeMake (200 .0f , 300 .0f );
412- CGRect stringRect;
413415 if ([string respondsToSelector: @selector (boundingRectWithSize:options:attributes:context: )]) {
414- stringRect = [string boundingRectWithSize: constraintSize
415- options: (NSStringDrawingOptions )(NSStringDrawingUsesFontLeading|NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesLineFragmentOrigin)
416- attributes: @{NSFontAttributeName : self.statusLabel .font }
417- context: NULL ];
416+ labelRect = [string boundingRectWithSize: constraintSize
417+ options: (NSStringDrawingOptions )(NSStringDrawingUsesFontLeading|NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesLineFragmentOrigin)
418+ attributes: @{NSFontAttributeName : self.statusLabel .font }
419+ context: NULL ];
418420 } else {
419421 CGSize stringSize;
420422 if ([string respondsToSelector: @selector (sizeWithAttributes: )]) {
@@ -427,75 +429,55 @@ - (void)updateHUDFrame {
427429#pragma clang diagnostic pop
428430#endif
429431 }
430- stringRect = CGRectMake (0 .0f , 0 .0f , stringSize.width , stringSize.height );
432+ labelRect = CGRectMake (0 .0f , 0 .0f , stringSize.width , stringSize.height );
431433 }
432-
433- CGFloat stringWidth = stringRect.size .width ;
434- CGFloat stringHeight = ceilf (CGRectGetHeight (stringRect));
434+ labelRect.size .width = MAX (self.minimumSize .width , horizontalSpacing + CGRectGetWidth (labelRect) + horizontalSpacing);
435+
436+ CGFloat labelHeight = ceilf (CGRectGetHeight (labelRect));
437+ CGFloat labelWidth = ceilf (CGRectGetWidth (labelRect));
435438
436439 if (imageUsed || progressUsed) {
437- hudHeight = stringAndContentHeightBuffer + stringHeight;
440+ CGFloat contentHeight = (imageUsed ? CGRectGetHeight (self.imageView .frame ) : CGRectGetHeight (self.indefiniteAnimatedView .frame ));
441+ hudHeight = verticalSpacing + contentHeight + progressLabelSpacing + labelHeight + verticalSpacing;
438442 } else {
439- hudHeight = stringHeightBuffer + stringHeight;
440- }
441- if (stringWidth > hudWidth) {
442- hudWidth = ceilf (stringWidth/2 )*2 ;
443- }
444- CGFloat labelRectY = (imageUsed || progressUsed) ? 68 .0f : 9 .0f ;
445- if (hudHeight > 100 .0f ) {
446- labelRect = CGRectMake (12 .0f , labelRectY, hudWidth, stringHeight);
447- hudWidth += 24 .0f ;
448- } else {
449- hudWidth += 24 .0f ;
450- labelRect = CGRectMake (0 .0f , labelRectY, hudWidth, stringHeight);
443+ hudHeight = verticalSpacing + labelHeight + verticalSpacing;
451444 }
445+ hudWidth = labelWidth;
452446 }
453447
454448 // Update values on subviews
455449 self.hudView .bounds = CGRectMake (0 .0f , 0 .0f , MAX (self.minimumSize .width , hudWidth), MAX (self.minimumSize .height , hudHeight));
456- labelRect.size .width += MAX (0 , self.minimumSize .width - hudWidth);
457450 [self updateBlurBounds ];
458451
459- if (string) {
460- self.imageView .center = CGPointMake (CGRectGetWidth (self.hudView .bounds )/2 , 36 .0f );
461- } else {
462- self.imageView .center = CGPointMake (CGRectGetWidth (self.hudView .bounds )/2 , CGRectGetHeight (self.hudView .bounds )/2 );
463- }
464-
465- self.statusLabel .hidden = NO ;
466- self.statusLabel .frame = labelRect;
467-
468452 // Animate value update
469453 [CATransaction begin ];
470454 [CATransaction setDisableActions: YES ];
471455
472- if (string) {
473- if (self.defaultAnimationType == SVProgressHUDAnimationTypeFlat) {
474- SVIndefiniteAnimatedView *indefiniteAnimationView = (SVIndefiniteAnimatedView*)self.indefiniteAnimatedView ;
475- indefiniteAnimationView.radius = self.ringRadius ;
476- [indefiniteAnimationView sizeToFit ];
477- }
478-
479- CGPoint center = CGPointMake ((CGRectGetWidth (self.hudView .bounds )/2 ), 36 .0f );
480- self.indefiniteAnimatedView .center = center;
456+ // Spinner and image view
457+ CGFloat centerY;
458+ if (string){
459+ CGFloat contentHeight = (imageUsed ? CGRectGetHeight (self.imageView .frame ) : CGRectGetHeight (self.indefiniteAnimatedView .frame ));
460+ CGFloat labelHeight = ceilf (CGRectGetHeight (labelRect));
461+ CGFloat yOffset = MAX (verticalSpacing, (self.minimumSize .height - contentHeight - progressLabelSpacing - labelHeight) / 2 .0f );
481462
482- if (self.progress != SVProgressHUDUndefinedProgress) {
483- self.backgroundRingView .center = self.ringView .center = CGPointMake ((CGRectGetWidth (self.hudView .bounds )/2 ), 36 .0f );
484- }
485- } else {
486- if (self.defaultAnimationType == SVProgressHUDAnimationTypeFlat) {
487- SVIndefiniteAnimatedView *indefiniteAnimationView = (SVIndefiniteAnimatedView*)self.indefiniteAnimatedView ;
488- indefiniteAnimationView.radius = self.ringNoTextRadius ;
489- [indefiniteAnimationView sizeToFit ];
490- }
491-
492- CGPoint center = CGPointMake ((CGRectGetWidth (self.hudView .bounds )/2 ), CGRectGetHeight (self.hudView .bounds )/2 );
493- self.indefiniteAnimatedView .center = center;
494-
495- if (self.progress != SVProgressHUDUndefinedProgress) {
496- self.backgroundRingView .center = self.ringView .center = CGPointMake ((CGRectGetWidth (self.hudView .bounds )/2 ), CGRectGetHeight (self.hudView .bounds )/2 );
497- }
463+ centerY = yOffset + contentHeight / 2 .0f ;
464+ } else {
465+ centerY = CGRectGetHeight (self.hudView .bounds ) / 2 .0f ;
466+ }
467+ self.indefiniteAnimatedView .center = CGPointMake ((CGRectGetWidth (self.hudView .bounds ) / 2 .0f ), centerY);
468+ if (self.progress != SVProgressHUDUndefinedProgress) {
469+ self.backgroundRingView .center = self.ringView .center = CGPointMake ((CGRectGetWidth (self.hudView .bounds ) / 2 .0f ), centerY);
498470 }
471+ self.imageView .center = CGPointMake (CGRectGetWidth (self.hudView .bounds ) / 2 .0f , centerY);
472+
473+ // Label
474+ if (imageUsed || progressUsed){
475+ labelRect.origin .y = (imageUsed ? CGRectGetMaxY (self.imageView .frame ) : CGRectGetMaxY (self.indefiniteAnimatedView .frame )) + progressLabelSpacing;
476+ } else {
477+ labelRect.origin .y = verticalSpacing;
478+ }
479+ self.statusLabel .frame = labelRect;
480+ self.statusLabel .hidden = !string;
499481
500482 [CATransaction commit ];
501483}
@@ -693,15 +675,17 @@ - (void)positionHUD:(NSNotification*)notification {
693675#if !defined(SV_APP_EXTENSIONS) && TARGET_OS_IOS
694676 self.frame = [[[UIApplication sharedApplication ] delegate ] window ].bounds ;
695677 UIInterfaceOrientation orientation = UIApplication.sharedApplication .statusBarOrientation ;
696- #elif !defined(SV_APP_EXTENSIONS)
697- self.frame = [UIApplication sharedApplication ].keyWindow .bounds ;
678+ #elif !defined(SV_APP_EXTENSIONS) && !TARGET_OS_IOS
679+ self.frame = [UIApplication sharedApplication ].keyWindow .bounds ;
698680#else
699681 if (self.viewForExtension ) {
700682 self.frame = self.viewForExtension .frame ;
701683 } else {
702684 self.frame = UIScreen.mainScreen .bounds ;
703685 }
686+ #if TARGET_OS_IOS
704687 UIInterfaceOrientation orientation = CGRectGetWidth (self.frame ) > CGRectGetHeight (self.frame ) ? UIInterfaceOrientationLandscapeLeft : UIInterfaceOrientationPortrait;
688+ #endif
705689#endif
706690
707691 // 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.
@@ -799,7 +783,7 @@ - (void)positionHUD:(NSNotification*)notification {
799783 __weak SVProgressHUD *weakSelf = self;
800784 [UIView animateWithDuration: animationDuration
801785 delay: 0
802- options: UIViewAnimationOptionAllowUserInteraction
786+ options: (UIViewAnimationOptions) ( UIViewAnimationOptionAllowUserInteraction | UIViewAnimationOptionBeginFromCurrentState)
803787 animations: ^{
804788 __strong SVProgressHUD *strongSelf = weakSelf;
805789 if (strongSelf) {
@@ -867,10 +851,17 @@ - (void)showProgress:(float)progress status:(NSString*)status {
867851 // Cancel the indefiniteAnimatedView, then show the ringLayer
868852 [strongSelf cancelIndefiniteAnimatedViewAnimation ];
869853
870- // Add ring to HUD and set progress
871- [strongSelf.hudView addSubview: strongSelf.ringView];
872- [strongSelf.hudView addSubview: strongSelf.backgroundRingView];
854+ // Add ring to HUD
855+ if (!strongSelf.ringView .superview )
856+ [strongSelf.hudView addSubview: strongSelf.ringView];
857+ if (!strongSelf.backgroundRingView .superview )
858+ [strongSelf.hudView addSubview: strongSelf.backgroundRingView];
859+
860+ // Set progress animated
861+ [CATransaction begin ];
862+ [CATransaction setDisableActions: YES ];
873863 strongSelf.ringView .strokeEnd = progress;
864+ [CATransaction commit ];
874865
875866 // Update the activity count
876867 if (progress == 0 ) {
@@ -988,7 +979,7 @@ __block void (^completionBlock)(void) = ^{
988979 if (strongSelf) {
989980 // Check if we really achieved to show the HUD (<=> alpha values are applied)
990981 // and the change of these values has not been cancelled in between
991- // e.g. due to a dismissmal
982+ // e.g. due to a dismissal
992983 if (strongSelf.alpha == 1 .0f && strongSelf.hudView .alpha == 1 .0f ){
993984 // Register observer <=> we now have to handle orientation changes etc.
994985 [strongSelf registerNotifications ];
0 commit comments