带背景的自定义 AlertView

发布于 2024-11-23 15:05:33 字数 1115 浏览 3 评论 0原文

大家,我需要在 UIAlertView 上设置一张图像..

在此处输入图像描述

我已附上我的 UIAlertview 和图像问题..

我已经使用了这个代码行..

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 = [theImage 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]];

请解决这个问题.. 我只需要带有警报的图像..

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

enter image description here

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 = [theImage 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..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

七度光 2024-11-30 15:05:33

试试这个...

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

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];
究竟谁懂我的在乎 2024-11-30 15:05:33

您应该考虑不使用 UIAlertView,而是拥有自己的 AlertView,请参阅 TSAlertView 作为替代实现,即不是从 UIAlertView 派生的。

TSAlertView 允许您设置自己的背景图像。


我不推荐的另一个解决方案可能是:

您可以使用 内省:循环遍历 UIAlertViews 子视图,识别保存背景图像的视图,将其隐藏并放置您自己的背景图像位于原始图像视图下方/上方的索引处。


我找到了这个项目:子类 UIAlertView 来自定义警报的外观。它不适用于 iOS 4.2+,但通过我的内省想法,您可以使其再次工作:

JKCustomAlert-layoutSubviews 更改为:

- (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);
}

我不承诺,这个技巧将在未来版本的 iOS 中发挥作用

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

涙—继续流 2024-11-30 15:05:33

我喜欢使用的解决方案是将 UIView 添加到 ViewController,它模仿警报的外观。
UIAlertView 的另一个属性是,在警报解除之前,无法使用应用程序的其他部分。通过使您的 UIView 成为另一个 UIView(具有清晰背景)的子视图(占据整个屏幕),可以轻松地模仿这一点。

如果您不想自己实现,可以使用以下自定义警报视图类:https://github。 com/shivsak/CustomAlertView

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文