带背景的自定义 AlertView
大家,我需要在 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..
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
试试这个...
Try this...
您应该考虑不使用 UIAlertView,而是拥有自己的 AlertView,请参阅 TSAlertView 作为替代实现,即不是从 UIAlertView 派生的。
TSAlertView 允许您设置自己的背景图像。
我不推荐的另一个解决方案可能是:
您可以使用 内省:循环遍历 UIAlertViews 子视图,识别保存背景图像的视图,将其隐藏并放置您自己的背景图像位于原始图像视图下方/上方的索引处。
我找到了这个项目:子类 UIAlertView 来自定义警报的外观。它不适用于 iOS 4.2+,但通过我的内省想法,您可以使其再次工作:
将
JKCustomAlert
的-layoutSubviews
更改为:我不承诺,这个技巧将在未来版本的 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
ofJKCustomAlert
to:I am NOT promising, that this trick will work in future versions of iOS
我喜欢使用的解决方案是将 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