从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.代码模拟点击

[java] view plaincopyprint?
  1. [AlertObject dismissWithClickedButtonIndex:0 animated:NO]