代码改变世界

关于自定义AlertView背景的方法收集

  张智清  阅读(4081)  评论(0编辑  收藏  举报

从网上收集了一些自定义AlertView背景的方法,汇总一下以便有需要时使用。

复制代码
UIAlertView *theAlert = [[[UIAlertView alloc] initWithTitle:@"Atention" message:@"我是中国人!" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil] autorelease];
[theAlert show];

// undocument API UIAlertView类头文件里面带 “ _”的成员是可以通过 valueforkey来引用的。但这些都是不公开的,私有方法
UILabel *theTitle = [theAlert valueForKey:@"_titleLabel"];
[theTitle setTextColor:[UIColor greenColor]];

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

/* 第一种自定义方法
//
UIImageView *imgv = [theAlert valueForKey:@"_backgroundImageView"];
imgv.image = [UIImage imageNamed:@"loveChina.png"];
*/
//

/* 第二种自定义方法,因有过期属性的使用,所以新版iOS中无效
//
// undocument API
UIImage *theImage = [UIImage imageNamed:@"loveChina.png"];
theImage = [theImage stretchableImageWithLeftCapWidth:0.0 topCapHeight:0.0];
CGSize theSize = [theAlert frame].size;

UIGraphicsBeginImageContext(theSize);
[theImage drawInRect:CGRectMake(0, 0, theSize.width, theSize.height)];
theImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
theAlert.layer.contents = (id)[theImage CGImage]; // iOS4.0开始不支持contents属性
*/
//

/* 第三种自定义方法
//遍历theAlert对象的子view,获取其UIImageView视图
for (UIView *v in [theAlert subviews]) {
if ([v isKindOfClass:[UIImageView class]]) {
UIImage *theImage = [UIImage imageNamed:@"loveChina.png"];
((UIImageView *)v).image = theImage;
}
}
*/

/* 第四种自定义方法 */
UIView *additionBackgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, theAlert.frame.size.width-30, theAlert.frame.size.height-20)];
additionBackgroundView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"loveChina.png"]];
#if TARGET_IPHONE_SIMULATOR
[theAlert insertSubview:additionBackgroundView atIndex:1];
#else
[theAlert insertSubview:additionBackgroundView atIndex:0];
#endif

[additionBackgroundView release];
复制代码

第五种自定义代码:

复制代码
 1 UIAlertView *theAlert = [[[UIAlertView alloc] initWithTitle:@"Atention" message:@"我是中国人!" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil] autorelease];
2 UIImage *alertImage = [UIImage imageNamed:@"loveChina.png"];
3 UIImageView *backgroundImageView = [[UIImageView alloc] initWithImage:alertImage];
4 backgroundImageView.frame = CGRectMake(0, 0, 282, 160);
5 backgroundImageView.contentMode = UIViewContentModeScaleToFill;
6 [theAlert addSubview:backgroundImageView];
7 [theAlert sendSubviewToBack:backgroundImageView];
8
9 [theAlert show];
10 [theAlert release];
复制代码

运行效果如图:

第六种方式:使用一个定义扩展类JKCustomAlert (网上有下载)。
调用代码:

UIImage *backgroundImage = [UIImage imageNamed:@"Splatter.png"];
alert = [[JKCustomAlert alloc] initWithImage:backgroundImage text:NSLocalizedString(@"game over", nil)];
[alert show];

运行效果图:


为了在iOS4.2以上也有效,需增加些代码来手动隐藏原AlertView的背景视图:修改layoutSubviews方法

复制代码
1 - (void) layoutSubviews {
2 for (UIView *v in [self subviews]) {
3 if ([v class] == [UIImageView class]) {
4 [v setHidden:YES];
5 }
6 }
7
8 //原来的代码继续
9 }
复制代码

 

编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
点击右上角即可分享
微信分享提示