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

从网上收集了一些自定义AlertView背景的方法,汇总一下以便有需要时使用。
  1. UIAlertView *theAlert = [[[UIAlertView alloc] initWithTitle:@"Atention" message:@"我是中国人!" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil] autorelease];

  2.     [theAlert show];

  3.    

  4.     // undocument API UIAlertView类头文件里面带 “ _”的成员是可以通过 valueforkey来引用的。但这些都是不公开的,私有方法

  5.     UILabel *theTitle = [theAlert valueForKey:@"_titleLabel"];

  6.     [theTitle setTextColor:[UIColor greenColor]];

  7.    

  8.     UILabel *theBody = [theAlert valueForKey:@"_bodyTextLabel"];

  9.     [theBody setTextColor:[UIColor blueColor]];

  10.    

  11.     /* 第一种自定义方法

  12.     //

  13.      UIImageView *imgv = [theAlert valueForKey:@"_backgroundImageView"];

  14.      imgv.image = [UIImage imageNamed:@"loveChina.png"];

  15.     */

  16.     //

  17.    

  18.     /* 第二种自定义方法,因有过期属性的使用,所以新版iOS中无效

  19.     //

  20.     // undocument API

  21.     UIImage *theImage = [UIImage imageNamed:@"loveChina.png"];

  22.     theImage = [theImage stretchableImageWithLeftCapWidth:0.0 topCapHeight:0.0];

  23.     CGSize theSize = [theAlert frame].size;

  24.    

  25.     UIGraphicsBeginImageContext(theSize);

  26.     [theImage drawInRect:CGRectMake(0, 0, theSize.width, theSize.height)];

  27.     theImage = UIGraphicsGetImageFromCurrentImageContext();

  28.     UIGraphicsEndImageContext();

  29.     theAlert.layer.contents = (id)[theImage CGImage]; // iOS4.0开始不支持contents属性

  30.     */

  31.     //

  32.    

  33.     /* 第三种自定义方法

  34.     //遍历theAlert对象的子view,获取其UIImageView视图

  35.     for (UIView *v in [theAlert subviews]) {

  36.         if ([v isKindOfClass:[UIImageView class]]) {

  37.             UIImage *theImage = [UIImage imageNamed:@"loveChina.png"];

  38.             ((UIImageView *)v).image = theImage;

  39.         }

  40.     }

  41.     */

  42.    

  43.     /* 第四种自定义方法 */

  44.     UIView *additionBackgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, theAlert.frame.size.width-30, theAlert.frame.size.height-20)];

  45.     additionBackgroundView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"loveChina.png"]];

  46. #if TARGET_IPHONE_SIMULATOR

  47.     [theAlert insertSubview:additionBackgroundView atIndex:1];

  48. #else

  49.     [theAlert insertSubview:additionBackgroundView atIndex:0];

  50. #endif

  51.    

  52.     [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. [theAlert show];

  9. [theAlert release];复制代码
复制代码

运行效果如图:


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

  1. UIImage *backgroundImage = [UIImage imageNamed:@"Splatter.png"];

  2. alert = [[JKCustomAlert alloc] initWithImage:backgroundImage text:NSLocalizedString(@"game over", nil)];

  3. [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. }复制代码
复制代码

链接地址; http://www.1000phone.net/thread-4630-1-1.html
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
自定义UIAlertView方法已经在iOS13之后被废弃,推荐使用UIAlertController来代替。下面介绍如何自定义UIAlertController的弹出位置以及宽度。 1. 自定义弹出位置 可以使用UIAlertController的popoverPresentationController属性来设置弹出位置。具体步骤如下: ``` // 创建UIAlertController对象 UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Title" message:@"Message" preferredStyle:UIAlertControllerStyleAlert]; // 创建弹出位置的UIView对象 UIView *popoverView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)]; // 设置弹出位置 alertController.popoverPresentationController.sourceView = popoverView; alertController.popoverPresentationController.sourceRect = popoverView.bounds; // 弹出UIAlertController [self presentViewController:alertController animated:YES completion:nil]; ``` 2. 自定义宽度 可以通过设置UIAlertController的preferredContentSize属性来改变其宽度。具体步骤如下: ``` // 创建UIAlertController对象 UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Title" message:@"Message" preferredStyle:UIAlertControllerStyleAlert]; // 设置宽度 alertController.preferredContentSize = CGSizeMake(280, 200); // 弹出UIAlertController [self presentViewController:alertController animated:YES completion:nil]; ``` 需要注意的是,此方法只适用于UIAlertControllerStyleAlert风格的UIAlertController。如果使用UIAlertControllerStyleActionSheet风格的UIAlertController,设置preferredContentSize属性将不会生效。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值