sberrevoets / SDCAlertView

The little alert that could
MIT License
1.86k stars 298 forks source link

Background color #66

Closed marcinjackowski closed 9 years ago

marcinjackowski commented 9 years ago

Hi, i have a big question for you ! It is possible to change background color in your library ? I'am looking for it all day but i couldn't change it. Please help me.. I want to do this on both ios 7 and 8. Please help me to do sth like this : ( background color looks like half tansparent )

zrzut ekranu 2014-11-17 o 14 32 30

marcinjackowski commented 9 years ago

My code :

SDCAlertController *alert = [SDCAlertController alertControllerWithTitle:@"Error"
                                                                             message:@"We couldn't recognise the QR code."
                                                                      preferredStyle:SDCAlertControllerStyleLegacyAlert];
            SDCAlertAction *alertAction = [SDCAlertAction actionWithTitle:@"OK" style:SDCAlertActionStyleDefault handler:nil];

            [alert addAction:alertAction];

            if(alert.legacyAlertView){

                // if i do aler alert.legacyAlertView.backgroundColor = [UIColor redColor]; nothing happens

                alert.legacyAlertView.buttonTextColor = [UIColor centimeterRed];
                alert.legacyAlertView.delegate = self;
            }else{

                // same here

                alert.view.tintColor = [UIColor purpleColor];
            }
            [alert presentWithCompletion:nil];
sberrevoets commented 9 years ago

That's not really supported right now, as the idea is to copy the system alert as much as possible.

Is there a particular reason you're trying to do this, or just because you want a different background?

marcinjackowski commented 9 years ago

Just because :) Diffrent question : Is your library at all support changing background color ? For example : All alertView will be black, both text color, and button text will be white. I know how to change text color but black alertview background is impossible, is it ?

sberrevoets commented 9 years ago

Yeah, the background is (in iOS 8) a UIVisualEffectView, which is pretty close to what the system alert looks like. You could try setting the backgroundColor there, but I doubt that works. It's not something that's supported right now, because it requires a completely separate code path and the alert won't look like an iOS alert anymore.

sberrevoets commented 9 years ago

Closing this as this is not, and probably won't be, something the alert will support. Let me know if you still have questions!

myster-t commented 9 years ago

One quick observation: it seems the SDCAlertController background color is a few shades darker than the iOS 8 alert view background color. If you look at the default alert views, they are first presented in a light light gray and then quickly flash to the slightly translucent background that blurs the views behind the alert. Is there a chance the default background color can be updated for ios 7/8?

sberrevoets commented 9 years ago

Unfortunately not. It's certainly not the same, but Apple internally uses a private class for alerts that uses a different visual effect than UIVisualEffectView. I haven't been able to figure out what exactly is going on there, so I had to settle for using UIVisualEffectView.

myster-t commented 9 years ago

So it seems you can update the backgroundColor of the appearance of UIVisualEffectView. You lose most of the blur/translucency effect, but you can tweak the tint of the background.

sberrevoets commented 9 years ago

Very interesting. Were you able to adjust its background to make it look more like the system alert?

myster-t commented 9 years ago

You get closer to the system alert popup. It's not a perfect match, but because the background is a few shades lighter it's harder to tell the difference. But like I mentioned earlier, setting a backgroundColor to the appearance appears to affect the blur effect... so pick one or the other hehe.

sberrevoets commented 9 years ago

Hmm, so that probably means that on light backgrounds (where the blur isn't as obvious) the one with a background color looks better, and on brighter background where the blur has a clear effect, the current one. Not sure what to pick then...

sberrevoets commented 9 years ago

@phaitour One of my coworkers today mentioned that if you set a tintColor on the UIVisualEffectView (instead of the backgroundColor) you don't actually lose the blur effect. I probably won't have time to try this for a while, but I figured I'd let you know in case you want to try it.

myster-t commented 9 years ago

@sberrevoets Thanks! I just tried it, but it didn't seem to work for me. The tint color affected the button text color, but not the background color. Although... I only tried setting the tint color to the UIVisualEffectView's appearance and not the tint color of the actual view object. Not sure if that would make a difference...

myster-t commented 9 years ago

@sberrevoets Interestingly, I used this to solve another problem I was experiencing.

I noticed that if you have multiple alerts shown on top of one another the non-foreground alerts get grayed out (in particular the button color). When these alerts come to the foreground, the button color goes back to the default blue and completely disregards the color set in the visual style.

So to solve this problem, I just set the button color to the UIVisualEffectView's tint color.

sberrevoets commented 9 years ago

Hmm, yeah that would be a problem. I'd appreciate a PR on that!

myster-t commented 9 years ago

Hmm, so changing the tint color has an adverse side effect: it forces the action button color to ignore any overriding values set in an attributed string. Because of this, I'm not submitting a PR for this... maybe we'll find another fix for the backgrounded alerts' buttons changing color down the road.

Additionally, I noticed that SDCAlertCollectionViewCell's updateWithAction:... function has a small bug. Because the font and textColor are set after an attributed string is set, it effectively overrides any font/text color settings in the attributed string itself. I'll attach a PR for this.