开发者

Custom AlertView With Background

Everybody, I need to set one image on UIAlertView..

Custom AlertView With Background

I have attached my UIAlertview with image prob..

i have used this code lines..

UIAlertView *theAlert = [[[UIAlertView alloc] initWithTitle:@"Atention"
   message: @"YOUR MESSAGE HERE", nil)
   delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease];

   [theAlert show];

   UILabel *theTitle = [theAlert valueForKey:@"_titleLabel"];
   [theTitle setTextColor:[UIColor redColor]];

   UILabel *theBody = [theAlert valueForKey:@"_bodyTextLabel"];
   [theBody setTextColor:[UIColor blueColor]];

   UIImage *theImage = [UIImage imageNamed:@"Background.png"];    
   theImage = [theImag开发者_如何学JAVAe stretchableImageWithLeftCapWidth:16 topCapHeight:16];
   CGSize theSize = [theAlert frame].size;

   UIGraphicsBeginImageContext(theSize);    
   [theImage drawInRect:CGRectMake(0, 0, theSize.width, theSize.height)];    
   theImage = UIGraphicsGetImageFromCurrentImageContext();    
   UIGraphicsEndImageContext();

   [[theAlert layer] setContents:[theImage CGImage]];

please solve this issue.. i need only image with alert..


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];


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜