关于自定义AlertView背景的方法收集
2011-12-24 22:32
513 查看
从网上收集了一些自定义AlertView背景的方法,汇总一下以便有需要时使用。
第五种自定义代码:
运行效果如图:
第六种方式:使用一个定义扩展类JKCustomAlert (网上有下载)。
调用代码:
运行效果图:
为了在iOS4.2以上也有效,需增加些代码来手动隐藏原AlertView的背景视图:修改layoutSubviews方法
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];
第五种自定义代码:
UIAlertView *theAlert = [[[UIAlertView alloc] initWithTitle:@"Atention" message:@"我是中国人!" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil] autorelease]; UIImage *alertImage = [UIImage imageNamed:@"loveChina.png"]; UIImageView *backgroundImageView = [[UIImageView alloc] initWithImage:alertImage]; backgroundImageView.frame = CGRectMake(0, 0, 282, 160); backgroundImageView.contentMode = UIViewContentModeScaleToFill; [theAlert addSubview:backgroundImageView]; [theAlert sendSubviewToBack:backgroundImageView]; [theAlert show]; [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方法
- (void) layoutSubviews { for (UIView *v in [self subviews]) { if ([v class] == [UIImageView class]) { [v setHidden:YES]; } } //原来的代码继续 }
相关文章推荐
- 关于自定义AlertView背景的方法收集
- 关于自定义AlertView背景的方法收集
- 关于自定义AlertView背景的方法收集
- 关于自定义AlertView背景的方法收集
- 关于自定义UIAlertView背景的方法收集
- 关于自定义AlertDialog,设置不能充满全屏的解决方法
- 关于anroid设置webview背景方法探讨(转)
- Android AlertDialog 方法setView(view,0,0,0,0)开发自定义对话框
- 关于自定义AlertDialog,设置不能充满全屏的解决方法
- Android填坑之旅(第十四篇)关于软键盘弹出未及时隐藏导致自定义View的onMeasure方法测量错误引发的血案
- 从AlertView例解设置view背景的几种方法
- 关于SearchView设置字体颜色,提示字体及背景的解决方法
- 关于自定义View初始化时不执行onDraw方法的笔记
- Android自定义View(一)关于super、this和构造方法
- 关于MVC中View使用自定义方法
- 关于实现自定义Dialog和实现Dialog里view的事件监听的两种方法
- 关于使用SurfaceView绘制同名背景为黑色解决方法
- 关于LayoutInflater.from().inflate()方法不能加载自定义view的解决办法
- 关于android自定义view的方法
- 自定义View,关于onMeasure方法的两个参数widthMeasureSpec,heightMeasureSpec