Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for dark mode on iOS 13 and above #1001

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
94 changes: 48 additions & 46 deletions Demo/Base.lproj/Main.storyboard

Large diffs are not rendered by default.

17 changes: 13 additions & 4 deletions Demo/Classes/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,19 @@ - (void)showErrorWithStatus {

- (IBAction)changeStyle:(id)sender {
UISegmentedControl *segmentedControl = (UISegmentedControl*)sender;
if(segmentedControl.selectedSegmentIndex == 0){
[SVProgressHUD setDefaultStyle:SVProgressHUDStyleLight];
} else {
[SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
switch(segmentedControl.selectedSegmentIndex){
case 0:
[SVProgressHUD setDefaultStyle:SVProgressHUDStyleLight];
break;
case 1:
[SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
break;
case 2:
[SVProgressHUD setDefaultStyle:SVProgressHUDStyleAutoLight];
break;
case 3:
[SVProgressHUD setDefaultStyle:SVProgressHUDStyleAutoDark];
break;
}
}

Expand Down
2 changes: 2 additions & 0 deletions Demo/SVProgressHUD-Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
<string>APPL</string>
<key>CFBundleVersion</key>
<string>1.0</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>NSMainNibFile~ipad</key>
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ As standard `SVProgressHUD` offers two preconfigured styles:

* `SVProgressHUDStyleLight`: White background with black spinner and text
* `SVProgressHUDStyleDark`: Black background with white spinner and text
* `SVProgressHUDStyleAutoLight`: Adjusts foreground and background colors in accordance with the user's appearance setting on iOS 13, falling back to `SVProgressHUDStyleLight` on prior versions of iOS.
* `SVProgressHUDStyleAutoDark`: Adjusts foreground and background colors in accordance with the user's appearance setting on iOS 13, falling back to `SVProgressHUDStyleDark` on prior versions of iOS.

If you want to use custom colors use `setForegroundColor` and `setBackgroundColor:`. These implicitly set the HUD's style to `SVProgressHUDStyleCustom`.

Expand Down
1 change: 1 addition & 0 deletions SVProgressHUD/SVIndefiniteAnimatedView.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

@interface SVIndefiniteAnimatedView : UIView

@property (nonatomic, assign) BOOL adjustsStrokeForTraitCollection;
@property (nonatomic, assign) CGFloat strokeThickness;
@property (nonatomic, assign) CGFloat radius;
@property (nonatomic, strong) UIColor *strokeColor;
Expand Down
41 changes: 27 additions & 14 deletions SVProgressHUD/SVIndefiniteAnimatedView.m
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ - (void)willMoveToSuperview:(UIView*)newSuperview {
- (void)layoutAnimatedLayer {
CALayer *layer = self.indefiniteAnimatedGradientLayer;
[self.layer addSublayer:layer];

CGFloat widthDiff = CGRectGetWidth(self.bounds) - CGRectGetWidth(layer.bounds);
CGFloat heightDiff = CGRectGetHeight(self.bounds) - CGRectGetHeight(layer.bounds);
layer.position = CGPointMake(CGRectGetWidth(self.bounds) - CGRectGetWidth(layer.bounds) / 2 - widthDiff / 2, CGRectGetHeight(self.bounds) - CGRectGetHeight(layer.bounds) / 2 - heightDiff / 2);
Expand All @@ -38,7 +38,7 @@ - (CAGradientLayer *)indefiniteAnimatedGradientLayer {
if (!_indefiniteAnimatedGradientLayer) {
CGPoint arcCenter = CGPointMake(self.radius+self.strokeThickness/2+5, self.radius+self.strokeThickness/2+5);
UIBezierPath* smoothedPath = [UIBezierPath bezierPathWithArcCenter:arcCenter radius:self.radius startAngle:(CGFloat) (M_PI*3/2) endAngle:(CGFloat) (-0.5 * M_PI) clockwise:NO];

CAShapeLayer *shapeLayer = [CAShapeLayer layer];
shapeLayer.contentsScale = [[UIScreen mainScreen] scale];
shapeLayer.frame = CGRectMake(0.0f, 0.0f, arcCenter.x*2, arcCenter.y*2);
Expand All @@ -50,7 +50,7 @@ - (CAGradientLayer *)indefiniteAnimatedGradientLayer {
shapeLayer.path = smoothedPath.CGPath;
shapeLayer.strokeStart = 0.4f;
shapeLayer.strokeEnd = 1.0f;

_indefiniteAnimatedGradientLayer = [CAGradientLayer layer];
_indefiniteAnimatedGradientLayer.startPoint = CGPointMake(0.5f, 0.0f);
_indefiniteAnimatedGradientLayer.endPoint = CGPointMake(0.5f, 1.0f);
Expand All @@ -66,10 +66,10 @@ - (CAGradientLayer *)indefiniteAnimatedGradientLayer {
@(1.0f),
nil];
_indefiniteAnimatedGradientLayer.mask = shapeLayer;

NSTimeInterval animationDuration = 1;
CAMediaTimingFunction *linearCurve = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];

CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
animation.fromValue = (id) 0;
animation.toValue = @(M_PI*2);
Expand All @@ -85,24 +85,24 @@ - (CAGradientLayer *)indefiniteAnimatedGradientLayer {
}

- (void)setFrame:(CGRect)frame {

if (!CGRectEqualToRect(frame, super.frame)) {
[super setFrame:frame];

if (self.superview) {
[self layoutAnimatedLayer];
}
}

}

- (void)setRadius:(CGFloat)radius {
if(radius != _radius) {
_radius = radius;

[_indefiniteAnimatedGradientLayer removeFromSuperlayer];
_indefiniteAnimatedGradientLayer = nil;

if (self.superview) {
[self layoutAnimatedLayer];
}
Expand All @@ -111,21 +111,21 @@ - (void)setRadius:(CGFloat)radius {

- (void)setStrokeColor:(UIColor*)strokeColor {
_strokeColor = strokeColor;

[_indefiniteAnimatedGradientLayer removeFromSuperlayer];
_indefiniteAnimatedGradientLayer = nil;

if (self.superview) {
[self layoutAnimatedLayer];
}
}

- (void)setStrokeThickness:(CGFloat)strokeThickness {
_strokeThickness = strokeThickness;

[_indefiniteAnimatedGradientLayer removeFromSuperlayer];
_indefiniteAnimatedGradientLayer = nil;

if (self.superview) {
[self layoutAnimatedLayer];
}
Expand All @@ -135,4 +135,17 @@ - (CGSize)sizeThatFits:(CGSize)size {
return CGSizeMake((self.radius+self.strokeThickness/2+5)*2, (self.radius+self.strokeThickness/2+5)*2);
}

- (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection {
if (@available(iOS 13.0, *)) {
if([self.traitCollection hasDifferentColorAppearanceComparedToTraitCollection:previousTraitCollection]
&& _adjustsStrokeForTraitCollection) {
_indefiniteAnimatedGradientLayer.colors = [NSArray arrayWithObjects:
(id)[self.strokeColor colorWithAlphaComponent:0.0f].CGColor,
(id)[self.strokeColor colorWithAlphaComponent:0.5f].CGColor,
(id)self.strokeColor.CGColor,
nil];
}
}
}

@end
1 change: 1 addition & 0 deletions SVProgressHUD/SVProgressAnimatedView.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

@interface SVProgressAnimatedView : UIView

@property (nonatomic, assign) BOOL adjustsStrokeForTraitCollection;
@property (nonatomic, assign) CGFloat radius;
@property (nonatomic, assign) CGFloat strokeThickness;
@property (nonatomic, strong) UIColor *strokeColor;
Expand Down
9 changes: 9 additions & 0 deletions SVProgressHUD/SVProgressAnimatedView.m
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,13 @@ - (CGSize)sizeThatFits:(CGSize)size {
return CGSizeMake((self.radius+self.strokeThickness/2+5)*2, (self.radius+self.strokeThickness/2+5)*2);
}

- (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection {
if (@available(iOS 13.0, *)) {
if([self.traitCollection hasDifferentColorAppearanceComparedToTraitCollection:previousTraitCollection]
&& _adjustsStrokeForTraitCollection) {
_ringAnimatedLayer.strokeColor = self.strokeColor.CGColor;
}
}
}

@end
8 changes: 5 additions & 3 deletions SVProgressHUD/SVProgressHUD.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ extern NSString * _Nonnull const SVProgressHUDDidAppearNotification;
extern NSString * _Nonnull const SVProgressHUDStatusUserInfoKey;

typedef NS_ENUM(NSInteger, SVProgressHUDStyle) {
SVProgressHUDStyleLight NS_SWIFT_NAME(light), // default style, white HUD with black text, HUD background will be blurred
SVProgressHUDStyleDark NS_SWIFT_NAME(dark), // black HUD and white text, HUD background will be blurred
SVProgressHUDStyleCustom NS_SWIFT_NAME(custom) // uses the fore- and background color properties
SVProgressHUDStyleLight NS_SWIFT_NAME(light), // default style, white HUD with black text, HUD background will be blurred
SVProgressHUDStyleDark NS_SWIFT_NAME(dark), // black HUD and white text, HUD background will be blurred
SVProgressHUDStyleCustom NS_SWIFT_NAME(custom), // uses the fore- and background color properties
SVProgressHUDStyleAutoLight NS_SWIFT_NAME(autoLight), // uses dark mode system setting falling back to light prior to iOS 13.
SVProgressHUDStyleAutoDark NS_SWIFT_NAME(autoDark) // uses dark mode system setting falling back to dark prior to iOS 13.
};

typedef NS_ENUM(NSUInteger, SVProgressHUDMaskType) {
Expand Down