从AlertView例解设置view背景的几种方法
来源:互联网 发布:mac 图标大小不一样 编辑:程序博客网 时间:2024/04/30 03:50
UIAlertView *alv_obj = [[[UIAlertView alloc] initWithTitle:@"AlertView" message:@"This is Example!" delegate:nil cancelButtonTitle:@"cancel" otherButtonTitles:@"confirm", nil] autorelease];
[alv_obj show];
// undocument API UIAlertView类头文件里面带 “ _”的成员是可以通过 valueforkey来引用的。但这些都是不公开的,私有方法 通过字典的方式取出其对象就可以操作其属性。如下:
UILabel *theTitle = [alv_obj valueForKey:@"_titleLabel"];
[theTitle setTextColor:[UIColor greenColor]];
UILabel *theBody = [alv_obj valueForKey:@"_bodyTextLabel"];
[theBody setTextColor:[UIColor blueColor]];
/* 第一种自定义方法*/
UIImageView *imgv = [alv_obj 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 = [alv_obj frame].size;
UIGraphicsBeginImageContext(theSize);
[theImage drawInRect:CGRectMake(0, 0, theSize.width, theSize.height)];
theImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
alv_obj.layer.contents = (id)[theImage CGImage]; // iOS4.0开始不支持contents属性
/* 第三种自定义方法*/
//遍历alv_obj对象的子view,获取其UIImageView视图
for (UIView *v in [alv_obj subviews]) {
if ([v isKindOfClass:[UIImageView class]]) {
UIImage *theImage = [UIImage imageNamed:@"loveChina.png"];
((UIImageView *)v).image = theImage;
}
}
/* 第四种自定义方法 */ UIView *additionBackgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, alv_obj.frame.size.width-30, alv_obj.frame.size.height-20)]; additionBackgroundView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"loveChina.png"]]; #if TARGET_IPHONE_SIMULATOR [alv_obj insertSubview:additionBackgroundView atIndex:1]; #else [alv_obj 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];
本质:作为复合控件,UIAlertView是由多个基本控件组合而成,每个基本控件的功能也是UIAlertView的功能的有效组成部分。所以,我们想修改UIAlertView的某个属性,可以通过修改控制UIAlertView的该属性的基本控件而达到目的。这是一个整体和局部的分析方法。例如本例,我们想修改UIAlertView的背景图,就是通过获取到控制UIAlertView的背景图的子控件UIImageView而达到目的的。
说点别的内容 uialertview 消失的调用方法
UIAlertView的消失本质其实是触发了一个dismiss事件。
触发这个事件有以下两种方法:
1.按钮点击,UIAlertView上如果有按钮,点击任何按钮都会触发该事件,UIAlertView消失;
2.代码模拟点击
- [AlertObject dismissWithClickedButtonIndex:0 animated:NO]
- 从AlertView例解设置view背景的几种方法
- 从AlertView例解设置view背景的几种方法
- 从AlertView例解设置view背景的几种方法
- 设置对话框背景的几种方法
- Android View的几种背景颜色设置
- 关于自定义AlertView背景的方法收集
- 关于自定义AlertView背景的方法收集
- 关于自定义AlertView背景的方法收集
- 关于自定义AlertView背景的方法收集
- 关于自定义AlertView背景的方法收集
- 设置view渐变背景的工具方法
- android背景颜色设置的几种方法
- 针对alertView弹出时的阴影背景的处理方法
- 设置一般View的背景
- OpenCV背景去除的几种方法
- OpenCV背景去除的几种方法
- OpenCV背景去除的几种方法
- OpenCV背景去除的几种方法
- 百度地图自定义弹窗(PopupOverlay)Android篇
- SPRING.NET 1.3.2 学习9--设置泛型集合的值
- Uboot
- 获取随机数函数sand()的总结
- termios 系列函数分析
- 从AlertView例解设置view背景的几种方法
- windows 获取操作系统版本信息
- C,C++宏中#与##的讲解
- 获取ios系统中已安装程序(MobileInstallation方式)
- Android ListView 分页加载
- ZJUT 1208 按绩点排名
- linux下文件的复制、移动与删除命令
- [python相关]用Python建立最简单的web服务器
- 练习小程序