从AlertView例解设置view背景的几种方法
2013-03-07 14:37
459 查看
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.代码模拟点击[java] view plaincopyprint?[AlertObject dismissWithClickedButtonIndex:0 animated:NO]
相关文章推荐
- 从AlertView例解设置view背景的几种方法
- IOS怎么设置View的背景会比较顺畅比较好的解决方法
- android在代码中四种设置控件(以及TextView的文字颜色)背景颜色的方法
- 几种设置UITableView的cell动态高度的方法
- ImagView设置图片的几种方法
- UIView设置背景图片的几种方法
- 关于anroid设置webview背景方法探讨
- WebView设置透明和设置背景图片的方法
- Qt常见设置背景图片的几种方法总结
- 针对alertView弹出时的阴影背景的处理方法
- 关于SearchView设置字体颜色,提示字体及背景的解决方法
- 设置对话框背景的几种方法
- QT中几种设置控件的图片背景的方法
- 控件背景设置为透明色的几种常用方法
- WebView设置透明和设置背景图片的方法
- 关于自定义AlertView背景的方法收集
- 关于自定义AlertView背景的方法收集
- WebView背景无法透明的设置方法
- 设置一个View的背景图片的集中方法
- android在代码中四种设置控件(以及TextView的文字颜色)背景颜色的方法