【问题标题】:Custom AlertView With Background带背景的自定义 AlertView
【发布时间】:2011-10-05 06:17:29
【问题描述】:

各位,我需要在 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]];

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

【问题讨论】:

  • 恕我直言 - 如果我看到那种警报视图,我会刺伤我的眼睛......
  • 所以,除了解决我的问题,请你有什么想法......

标签: objective-c ios cocoa-touch uikit uialertview


【解决方案1】:

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

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


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

您可以使用introspection:循环遍历 UIAlertViews 子视图,确定将背景图像设置为隐藏的那个,并将您自己的背景图像放置在原始图像视图下方/上方的索引处。


我找到了这个项目:Subclass UIAlertView to customize the look of an alert。它不适用于 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 版本中起作用

【讨论】:

  • Apple 不希望 UIAlertView 的样式被改变。使用自定义 AlertView 是最简洁的方法。
  • 感谢您使用苹果指南更新我...但是它可以制作警报视图,例如为 iPhone 游戏添加图像...
  • 查看我的最新编辑。它正在使用 UIAlertView 的子类,对你来说应该绝对没问题。但是,如果您不想子类化,则可以在显示之前对常规 UIAlertView 进行操作。
【解决方案2】:

试试这个...

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

【讨论】:

  • 默认图片关闭时仍能看到其轮廓。
【解决方案3】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-18
    • 1970-01-01
    • 2011-10-30
    • 2010-10-16
    相关资源
    最近更新 更多