Custom AlertView With Background

青春壹個敷衍的年華 提交于 2019-11-29 08:08:40
userar

Try this...

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"UIAlert View" message:@"hello" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:@"Close",nil];

UIImage *alertImage = [UIImage imageNamed:@"plus.png"];

UIImageView *backgroundImageView = [[UIImageView alloc] initWithImage:alertImage];

backgroundImageView.frame = CGRectMake(0, 0, 282, 130);

backgroundImageView.contentMode = UIViewContentModeScaleToFill;

[alert addSubview:backgroundImageView];

[alert sendSubviewToBack:backgroundImageView]; 
[alert show];
[alert release];
vikingosegundo

You should consider to not use UIAlertView, but have your own AlertView see TSAlertView for an alternative implementation, that is not derived from UIAlertView.

TSAlertView is allowing you to set your own background image.


Another solution that I am not recommending could be:

You can use introspection: Loop over the UIAlertViews subviews, identify the one that holds the background image set it hidden and place your own backroundimage at an index below/over the original image view.


I found this project: Subclass UIAlertView to customize the look of an alert. It is not working for iOS 4.2+, but with my introspection idea you can make it work again:

change the -layoutSubviews of JKCustomAlert to:

- (void) layoutSubviews {
    for (UIView *v in [self subviews]) {
        if ([v class] == [UIImageView class]) {
            [v setHidden:YES];
        }
    }
    alertTextLabel.transform = CGAffineTransformIdentity;
    [alertTextLabel sizeToFit];

    CGRect textRect = alertTextLabel.frame;
    textRect.origin.x = (CGRectGetWidth(self.bounds) - CGRectGetWidth(textRect)) / 2;
    textRect.origin.y = (CGRectGetHeight(self.bounds) - CGRectGetHeight(textRect)) / 2;
    textRect.origin.y -= 30.0;

    alertTextLabel.frame = textRect;

    alertTextLabel.transform = CGAffineTransformMakeRotation(- M_PI * .08);
}

I am NOT promising, that this trick will work in future versions of iOS

A solution I like to use for this is to add a UIView to the ViewController, which mimics the appearance of an alert. The other property of a UIAlertView is that no other part of the app can be used until the alert is dismissed. This can easily be mimicked by making your UIView a subview of another UIView (with a clear background), which takes up the entire screen.

If you don't want to implement that yourself, here's a Custom Alert View class you could use: https://github.com/shivsak/CustomAlertView

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!