how can I have a totally transparent UIAlertView?

2019-03-05 07:34发布

问题:

I need a UIALertView, which on popping up should be transparent enough to show the background behind the alertview in a faded way and the blue color of the alert view should not appear since the background behind the alert view is black and grey in color. Changing color and putting an image to the alert view is one thing which can be seen in the links : iOS 5 black transparent UIAlertView and Changing the background color of a UIAlertView?
but I need a transparent UIAlertView. Is it possible to get one?If yes how? Thanks for the help in advance.

回答1:

If you look at Apple's documentation page for UIAlertView you'll see this note:

The UIAlertView class is intended to be used as-is and does not support subclassing. The view hierarchy for this class is private and must not be modified.

In other words, Apple doesn't want developers to muck around with colors or fonts or backgrounds when it comes to UIAlertView. If Apple changes the internal guts of UIAlertView in a later iOS, your app is likely to have unexpected and not-desired effects when that modified alert appears.

If you want custom UIAlertView like behavior, why not just write your own UIView that looks and behaves a lot like UIAlertView. And then you have full control over everything about how it's drawn.



回答2:

As Michael said, you can't with an UIAlertView, you have to build it yourself.

You can try BlockAlertsAnd-ActionSheets, it is like a customizable UIAlerView https://github.com/gpambrozio/BlockAlertsAnd-ActionSheets



回答3:

I know it's too late but, I'll leave this command for other developers.

.h

@interface CustomAlertView:UIAlertView {}

.m

// override
- (void)drawRect:(CGRect)rect
{
    for(UIView * sub in self.subviews)
    {
        [sub removeFromSuperview];
    }
    // this removeFromSuperview will clear all thing such as background view and buttons

    // do something
}