众所周知,当伟大的iOS7系统发布后,表扬的一堆、谩骂的也一片,而对于我们程序员来说最关心的莫过于低版本系统上的程序在搞版本系统上的兼容性问题了。

在iOS6.1几之前,当我们想要做一些提醒用户或临时获取一些数据时,通常会弹出一个模态试图,给予用户提醒,而最常见的做法莫过于直接用UIAlertView添加控件或继承UIAlertView,然后添加自己想要的控件,如:在执行网络连接  下载等耗时任务时我们会弹出一个view  然后显示一个指示器,具体做法:

  1. - (IBAction)showTraditionAlert:(id)sender {
  2.  
  3. UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"正在下载....." message:nil delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];
  4. alertView.delegate = self;
  5. [alertView show];
  6. }
  7.  
  8. #pragma mark -- UIAlertViewDelegate
  9.  
  10. //实现代理增加网络指示器
  11. - (void)willPresentAlertView:(UIAlertView *)alertView;{
  12. indicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
  13. indicator.frame = CGRectMake(110, 20, 50, 50);
  14.  
  15. [alertView addSubview:indicator];
  16. [indicator startAnimating];
  17. [indicator release];
  18. }
  19.  
  20. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
  21. [indicator stopAnimating];
  22. }

但上面的做法到了iOS7上就没有用了 ,你自己所添加的控件根本显示不出来,也就是说在iOS7上不允许我们更改系统的UIAlertView了(至少目前是这样2013/07/18 beta3版本),我想大家肯定也遇到了这样的问题,那现在改怎么半呢? 可以采用UIWindow的方式实现具体做法网上很多,我比较懒 随便写了点

  1. //
  2. // CustomizedAlertAnimation.h
  3. // AlertAnimationDemo
  4. //
  5. // Created by PSH_Chen_Tao on 7/18/13.
  6. // Copyright (c) 2013 wolfman. All rights reserved.
  7. //
  8.  
  9. //这个类主要时用来对指定的view进行动画,,动画类似UIAlertView的出现和消失
  10. #import <Foundation/Foundation.h>
  11.  
  12. @protocol CustomizedAlertAnimationDelegate;
  13.  
  14. @interface CustomizedAlertAnimation : NSObject
  15.  
  16. @property(strong,nonatomic)UIView *view;
  17.  
  18. @property(assign,nonatomic)id<CustomizedAlertAnimationDelegate> delegate;
  19.  
  20. -(id)customizedAlertAnimationWithUIview:(UIView *)v;
  21.  
  22. -(void)showAlertAnimation;
  23.  
  24. -(void)dismissAlertAnimation;
  25. @end
  26.  
  27. @protocol CustomizedAlertAnimationDelegate
  28.  
  29. -(void)showCustomizedAlertAnimationIsOverWithUIView:(UIView *)v;
  30.  
  31. -(void)dismissCustomizedAlertAnimationIsOverWithUIView:(UIView *)v;
  32. @end
  1. //
  2. // CustomizedAlertAnimation.m
  3. // AlertAnimationDemo
  4. //
  5. // Created by PSH_Chen_Tao on 7/18/13.
  6. // Copyright (c) 2013 wolfman. All rights reserved.
  7. //
  8.  
  9. #import "CustomizedAlertAnimation.h"
  10.  
  11. static CGFloat kTransitionDuration = 0.3;
  12.  
  13. @implementation CustomizedAlertAnimation
  14.  
  15. @synthesize view;
  16.  
  17. @synthesize delegate;
  18.  
  19. -(void)dealloc{
  20. if (delegate) {
  21. delegate = nil;
  22.  
  23. }
  24. [view release];
  25. view = nil;
  26. [super dealloc];
  27. }
  28.  
  29. -(id)customizedAlertAnimationWithUIview:(UIView *)v{
  30. if (self=[super init]) {
  31. view = v;
  32.  
  33. }
  34. return self;
  35. }
  36.  
  37. //get the transform of view based on the orientation of device.
  38.  
  39. -(CGAffineTransform)transformForOrientation{
  40. CGAffineTransform transform ;
  41. UIInterfaceOrientation orientation = [[UIApplication sharedApplication ]statusBarOrientation];
  42.  
  43. switch (orientation) {
  44. case UIInterfaceOrientationLandscapeLeft:
  45. transform = CGAffineTransformMakeRotation(M_PI*1.5);
  46. break;
  47. case UIInterfaceOrientationLandscapeRight:
  48. transform = CGAffineTransformMakeRotation(M_PI/2);
  49. break;
            //这里写错了,感谢 阿秉 提出问题,当为倒置方向时才应该旋转
  50. //case UIInterfaceOrientationPortrait:
           case UIInterfaceOrientationPortraitUpsideDown:
  1. transform = CGAffineTransformMakeRotation(-M_PI);
  2. break;
  3. default:
  4. transform = CGAffineTransformIdentity;
  5. break;
  6. }
  7.  
  8. return transform;
  9. }
  10.  
  11. // begin the animation
  12.  
  13. -(void)showAlertAnimation{
  14. view.transform = CGAffineTransformScale([self transformForOrientation], 0.001, 0.001);
  15. [UIView beginAnimations:nil context:nil];
  16. [UIView setAnimationDuration:kTransitionDuration/1.5];
  17. [UIView setAnimationDelegate:self];
  18. [UIView setAnimationDidStopSelector:@selector(firstBouncesDidStop)];
  19. view.transform = CGAffineTransformScale([self transformForOrientation], 1.1, 1.1);
  20. [UIView commitAnimations];
  21. }
  22.  
  23. -(void)dismissAlertAnimation{
  24. [UIView beginAnimations:nil context:nil];
  25. [UIView setAnimationDuration:kTransitionDuration/2];
  26. [UIView setAnimationDelegate:self];
  27. view.alpha = 0;
  28. [UIView setAnimationDidStopSelector:@selector(dismissAlertAnimationDidStoped)];
  29. [UIView commitAnimations];
  30. }
  31.  
  32. #pragma mark -- UIViewAnimation delegate
  33.  
  34. -(void)firstBouncesDidStop{
  35. [UIView beginAnimations:nil context:nil];
  36. [UIView setAnimationDuration:kTransitionDuration/2];
  37. [UIView setAnimationDelegate:self];
  38. [UIView setAnimationDidStopSelector:@selector(secondBouncesDidStop)];
  39. view.transform = CGAffineTransformScale([self transformForOrientation], 0.9, 0.9);
  40. [UIView commitAnimations];
  41.  
  42. }
  43.  
  44. -(void)secondBouncesDidStop{
  45. [UIView beginAnimations:nil context:nil];
  46. [UIView setAnimationDelegate:self];
  47. [UIView setAnimationDuration:kTransitionDuration/2];
  48. view.transform = [self transformForOrientation];
  49. [UIView commitAnimations];
  50.  
  51. //You can do somethings at the end of animation
  52.  
  53. [self.delegate showCustomizedAlertAnimationIsOverWithUIView:view];
  54. }
  55.  
  56. -(void)dismissAlertAnimationDidStoped{
  57. [self.delegate dismissCustomizedAlertAnimationIsOverWithUIView:view];
  58. }
  59. @end
  1. //
  2. // ;
  3. // AlertDemo
  4. //
  5. // Created by PSH_Chen_Tao on 7/19/13.
  6. // Copyright (c) 2013 wolfman. All rights reserved.
  7. //
  8.  
  9. //自定义的 alert view 类
  10.  
  11. #import <Foundation/Foundation.h>
  12.  
  13. #import "CustomizedAlertAnimation.h"
  14.  
  15. @protocol CustomeAlertViewDelegate ;
  16.  
  17. @interface CustomeAlertView : UIWindow <CustomizedAlertAnimationDelegate>
  18.  
  19. @property(strong,nonatomic)UIView *myView;
  20.  
  21. @property(strong,nonatomic)UIActivityIndicatorView *activityIndicator;
  22.  
  23. @property(strong,nonatomic)CustomizedAlertAnimation *animation;
  24.  
  25. @property(assign,nonatomic)id<CustomeAlertViewDelegate> delegate;
  26. -(void)show;
  27. @end
  28.  
  29. @protocol CustomeAlertViewDelegate
  30.  
  31. -(void)CustomeAlertViewDismiss:(CustomeAlertView *) alertView;
  32.  
  33. @end
  1. //
  2. // CustomeAlertView.m
  3. // AlertDemo
  4. //
  5. // Created by PSH_Chen_Tao on 7/19/13.
  6. // Copyright (c) 2013 wolfman. All rights reserved.
  7. //
  8.  
  9. #import "CustomeAlertView.h"
  10.  
  11. @implementation CustomeAlertView
  12. @synthesize myView;
  13. @synthesize activityIndicator;
  14. @synthesize animation;
  15. @synthesize delegate;
  16. -(id)init{
  17. if (self=[super init]) {
  18. self.frame = [[UIScreen mainScreen] bounds];
  19. self.backgroundColor = [UIColor clearColor];
  20. //UIWindow的层级 总共有三种
  21. self.windowLevel = UIWindowLevelAlert;
  22. myView = [[UIView alloc]initWithFrame:CGRectMake(30, 140, 260, 200)];
  23. UIButton *okButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
  24. [okButton setBackgroundImage:[UIImage imageNamed:@"alert-view-ok-button"] forState:UIControlStateNormal];
  25. [okButton addTarget:self action:@selector(pressoKButton:) forControlEvents:UIControlEventTouchUpInside];
  26. okButton.frame = CGRectMake(90, 130, 80, 40);
  27. [myView addSubview:okButton];
  28. // [okButton release];
  29.  
  30. activityIndicator = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(105, 75, 50, 50)];
  31. activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhite;
  32. [myView addSubview:activityIndicator];
  33. // [activityIndicator release];
  34. UIImageView *imageView = [[UIImageView alloc]initWithFrame:myView.bounds];
  35. [imageView setImage:[[UIImage imageNamed:@"alert-view-bg-portrait"] stretchableImageWithLeftCapWidth:100 topCapHeight:30]];
  36. [myView insertSubview:imageView atIndex:0];
  37. [imageView release];
  38.  
  39. animation = [[CustomizedAlertAnimation alloc]customizedAlertAnimationWithUIview:myView];
  40. animation.delegate = self;
  41. [self addSubview:myView];
  42. [myView release];
  43. }
  44.  
  45. return self;
  46. }
  47.  
  48. -(void)show{
  49. [self makeKeyAndVisible];
  50. [animation showAlertAnimation];
  51. }
  52.  
  53. -(void)dismiss{
  54.  
  55. [self resignKeyWindow];
  56. [animation dismissAlertAnimation];
  57.  
  58. }
  59.  
  60. -(void) pressoKButton:(id)sender{
  61. [self dismiss];
  62. }
  63.  
  64. #pragma mark -- CustomizedAlertAnimationDelegate
  65.  
  66. //自定义的alert view出现动画结束后调用
  67. -(void)showCustomizedAlertAnimationIsOverWithUIView:(UIView *)v{
  68. NSLog(@"showCustomizedAlertAnimationIsOverWithUIView");
  69. [activityIndicator startAnimating];
  70. }
  71.  
  72. //自定义的alert view消失动画结束后调用
  73. -(void)dismissCustomizedAlertAnimationIsOverWithUIView:(UIView *)v{
  74. NSLog(@"dismissCustomizedAlertAnimationIsOverWithUIView");
  75. [activityIndicator stopAnimating];
  76.  
  77. [self.delegate CustomeAlertViewDismiss:self];
  78.  
  79. }
  80.  
  81. @end
  1. //
  2. // ViewController.h
  3. // AlertDemo
  4. //
  5. // Created by PSH_Chen_Tao on 7/19/13.
  6. // Copyright (c) 2013 wolfman. All rights reserved.
  7. //
  8.  
  9. #import <UIKit/UIKit.h>
  10.  
  11. #import "CustomeAlertView.h"
  12. @interface ViewController : UIViewController <UIAlertViewDelegate,CustomeAlertViewDelegate>
  13. - (IBAction)showTraditionAlert:(id)sender;
  14. - (IBAction)showWindowAlert:(id)sender;
  15.  
  16. @property(strong,nonatomic) UIActivityIndicatorView *indicator;
  17.  
  18. @property(strong,nonatomic)CustomeAlertView *customeAlertView;
  19. @end
  1. //
  2. // ViewController.m
  3. // AlertDemo
  4. //
  5. // Created by PSH_Chen_Tao on 7/19/13.
  6. // Copyright (c) 2013 wolfman. All rights reserved.
  7. //
  8.  
  9. #import "ViewController.h"
  10.  
  11. @interface ViewController ()
  12.  
  13. @end
  14.  
  15. @implementation ViewController
  16.  
  17. @synthesize indicator;
  18.  
  19. @synthesize customeAlertView;
  20.  
  21. - (void)viewDidLoad
  22. {
  23. [super viewDidLoad];
  24. // Do any additional setup after loading the view, typically from a nib.
  25.  
  26. }
  27.  
  28. - (void)didReceiveMemoryWarning
  29. {
  30. [super didReceiveMemoryWarning];
  31. // Dispose of any resources that can be recreated.
  32. }
  33.  
  34. - (IBAction)showTraditionAlert:(id)sender {
  35.  
  36. UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"正在下载....." message:nil delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];
  37. alertView.delegate = self;
  38. [alertView show];
  39. }
  40.  
  41. - (IBAction)showWindowAlert:(id)sender {
  42.  
  43. customeAlertView = [[CustomeAlertView alloc]init];
  44. customeAlertView.delegate = self;
  45.  
  46. [customeAlertView show];
  47. }
  48.  
  49. #pragma mark -- UIAlertViewDelegate
  50.  
  51. //实现代理增加网络指示器
  52. - (void)willPresentAlertView:(UIAlertView *)alertView;{
  53. indicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
  54. indicator.frame = CGRectMake(, , , );
  55.  
  56. [alertView addSubview:indicator];
  57. [indicator startAnimating];
  58. [indicator release];
  59. }
  60.  
  61. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
  62. [indicator stopAnimating];
  63. }
  64.  
  65. #pragma mark -- CustomeAlertViewDelegate
  66.  
  67. -(void)CustomeAlertViewDismiss:(CustomeAlertView *) alertView{
  68. [alertView release];
  69.  
  70. NSLog(@"CustomeAlertViewDismiss");
  71. }
  72. @end

对于UIWindow的相关东西可以参考  http://www.cnblogs.com/smileEvday/archive/2012/03/27/2420362.html#2728097

注意设计到UIWindow显示的工程不能用arc ,具体原因还没找到让我信服的 ,希望知道的大牛可以回复下。。。。。。。

自定义的UIAlertView不能在iOS7上正常显示的更多相关文章

  1. IOS 7 自定义的UIAlertView不能在iOS7上正常显示

    本文转载至 http://blog.csdn.net/hanbing861210/article/details/13614405 众所周知,当伟大的iOS7系统发布后,表扬的一堆.谩骂的也一片,而对 ...

  2. 自定义plain 样式的 tableview,模拟器上不显示分割线,真机上却显示分割线.

    一, 经历 1> 自定义plain 样式的 tableview,模拟器上不显示分割线,真机上却显示cell 下面的分割线. 2> 尝试使用表格的separatorStyle属性,尝试失败. ...

  3. iOS7上leftBarButtonItem无法实现滑动返回的完美解决方案

    今天遇到了在iOS7上使用leftBarButtonItem却无法响应滑动返回事件的问题,一番谷歌,最后终于解决了,在这里把解决方案分享给大家. 在iOS7之前的系统,如果要自定义返回按钮,直接设置b ...

  4. iOS7上的地图定位接口BUG

    遇到个BUG,卡了好久,就是在iOS9上定位接口是正常的,但是在iOS7上就一直拿不到回调,但是看系统日志其实已经定位到了.总是在报一句not response,也没有具体函数名 昨天灵机一动,从de ...

  5. ios7 上 UIActivity 用的image

    在ios8 上UIActivityCategoryShare类型的UIActivity的图标支持彩色图片了,但是在ios7上不行,ios8上的 UIActivityCategoryAction类型也不 ...

  6. iOS7上TableViewCell的button和UIImageView个别未显示的bug

    要做这个cell,用xib将cell做成之后,在iPhone6.6Plus.5s上运行良好,但是在iOS7的5s和iPad上,黄色的小星星和下载按钮均没有显示. 甚为惊奇. 在网上百度之,发现了解决办 ...

  7. phpcms v9自定义表单提交后返回上一页实现方法

    PHPcms v9中提交自定义表单后默认都是回到首页的,是不是感觉很不爽! 接下来,就说下phpcms v9自定义表单提交后返回上一页实现方法. 1.找到这个文件 phpcms\modules\for ...

  8. IOS7上呈现IOS6的水滴刷新效果

    IOS7上呈现IOS6的水滴刷新效果    到了IOS7 发现自带的刷新 不再是 IOS6自带的水滴效果了 你是否怀念那IOS6的效果呢? 哈哈,于是收集各方资料,整理编写一个属于自己的水滴刷新效果 ...

  9. ios7上隐藏status bar

    在iOS7上 对于设置status bar 又有了点点的改变 1.对于 UIViewController 加入了动态改变 status bar style的方法 - (UIStatusBarStyle ...

随机推荐

  1. HTML在Select具体的使用说明

    <html> <head> <SCRIPT LANGUAGE="JavaScript"> <!-- //oSelect 列表的底部加入了一 ...

  2. 关于”机器学习方法“,&quot;深度学习方法&quot;系列

    "机器学习/深度学习方法"系列,我本着开放与共享(open and share)的精神撰写,目的是让很多其它的人了解机器学习的概念,理解其原理,学会应用.如今网上各种技术类文章非常 ...

  3. winhec

    #winhec# 开发人员刷屏看点 (视频) 今天大家已经被winhec刷屏了,本来不想写这篇了,但看了所有的文章,大家关注的都是windows 10的那些新功能,小米win10刷机,联想千元手机,小 ...

  4. c# 文件同步服务器,iis 集群 ,代码同步(二)

    打算自己做一个 集群 代码同步软件. 基于Socket 和 FileSystemWatcher .

  5. Fckeditor用法

    试Fckeditor版本号:2.6.3 眼下Fckeditor仅仅能用于基于ie内核的浏览器,假设要使用于chrome等浏览器,请使用ckeditor. 详细用法: 1.将解压后的fckeditor整 ...

  6. fast-json.jar的用法

    fast-json.jar 解析json数据:一种json数据解析方式是这种,点击这里下载jsonfast.jar+fastjsonAPI文档 [ { "id": 6378, &q ...

  7. webkit的几个属性

    -webkit-text-size-adjust 1.当样式表里font-size<12px时,中文版chrome浏览器里字体显示仍为12px,这时可以用 html{-webkit-text-s ...

  8. c语言mysql api

    原文:c语言mysql api 1.mysql_affected_rows()            //返回上次UPDATE.DELETE或INSERT查询更改/删除/插入的行数. 2.mysql_ ...

  9. mvc拦截器

    在ASP.NET MVC中,有三种拦截器:Action拦截器.Result拦截器和Exception拦截器.这里说的是第一种和第三种.其实所谓的ASP.NET MVC拦截器,也没什么神秘的,就是一个普 ...

  10. 你不明白 String 类别

    序 前几篇文章一直在研究 WEB 安全的问题,并且近期工作中也一直在解决这些安全漏洞.当然,兴许的博客中还会就这些暴露出来的问题谈谈我的解决方式,仅仅只是,在这篇博客中,我想谈点别的问题.并且对于我们 ...