IOS 7 自定义的UIAlertView不能在iOS7上正常显示
本文转载至 http://blog.csdn.net/hanbing861210/article/details/13614405
众所周知,当伟大的iOS7系统发布后,表扬的一堆、谩骂的也一片,而对于我们程序员来说最关心的莫过于低版本系统上的程序在搞版本系统上的兼容性问题了。
在iOS6.1几之前,当我们想要做一些提醒用户或临时获取一些数据时,通常会弹出一个模态试图,给予用户提醒,而最常见的做法莫过于直接用UIAlertView添加控件或继承UIAlertView,然后添加自己想要的控件,如:在执行网络连接 下载等耗时任务时我们会弹出一个view 然后显示一个指示器,具体做法:
- (IBAction)showTraditionAlert:(id)sender { UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"正在下载....." message:nil delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil]; alertView.delegate = self; [alertView show];}#pragma mark -- UIAlertViewDelegate//实现代理增加网络指示器- (void)willPresentAlertView:(UIAlertView *)alertView;{ indicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite]; indicator.frame = CGRectMake(110, 20, 50, 50); [alertView addSubview:indicator]; [indicator startAnimating]; [indicator release];}- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ [indicator stopAnimating];} |
但上面的做法到了iOS7上就没有用了 ,你自己所添加的控件根本显示不出来,也就是说在iOS7上不允许我们更改系统的UIAlertView了(至少目前是这样2013/07/18 beta3版本),我想大家肯定也遇到了这样的问题,那现在改怎么半呢? 可以采用UIWindow的方式实现具体做法网上很多,我比较懒 随便写了点
//// CustomizedAlertAnimation.h// AlertAnimationDemo//// Created by PSH_Chen_Tao on 7/18/13.// Copyright (c) 2013 wolfman. All rights reserved.////这个类主要时用来对指定的view进行动画,,动画类似UIAlertView的出现和消失#import <Foundation/Foundation.h>@protocol CustomizedAlertAnimationDelegate;@interface CustomizedAlertAnimation : NSObject@property(strong,nonatomic)UIView *view;@property(assign,nonatomic)id<CustomizedAlertAnimationDelegate> delegate;-(id)customizedAlertAnimationWithUIview:(UIView *)v;-(void)showAlertAnimation;-(void)dismissAlertAnimation;@end@protocol CustomizedAlertAnimationDelegate-(void)showCustomizedAlertAnimationIsOverWithUIView:(UIView *)v;-(void)dismissCustomizedAlertAnimationIsOverWithUIView:(UIView *)v;@end |
//// CustomizedAlertAnimation.m// AlertAnimationDemo//// Created by PSH_Chen_Tao on 7/18/13.// Copyright (c) 2013 wolfman. All rights reserved.//#import "CustomizedAlertAnimation.h"static CGFloat kTransitionDuration = 0.3;@implementation CustomizedAlertAnimation@synthesize view;@synthesize delegate;-(void)dealloc{ if (delegate) { delegate = nil; } [view release]; view = nil; [super dealloc];}-(id)customizedAlertAnimationWithUIview:(UIView *)v{ if (self=[super init]) { view = v; } return self;}//get the transform of view based on the orientation of device.-(CGAffineTransform)transformForOrientation{ CGAffineTransform transform ; UIInterfaceOrientation orientation = [[UIApplication sharedApplication ]statusBarOrientation]; switch (orientation) { case UIInterfaceOrientationLandscapeLeft: transform = CGAffineTransformMakeRotation(M_PI*1.5); break; case UIInterfaceOrientationLandscapeRight: transform = CGAffineTransformMakeRotation(M_PI/2); break;<br> //这里写错了,感谢 <a id="a_comment_author_2783455" href="http://home.cnblogs.com/u/570796/" target="_blank">阿秉</a> 提出问题,当为倒置方向时才应该旋转 //case UIInterfaceOrientationPortrait:<br> case UIInterfaceOrientationPortraitUpsideDown: |
transform = CGAffineTransformMakeRotation(-M_PI); break; default: transform = CGAffineTransformIdentity; break; } return transform;}// begin the animation-(void)showAlertAnimation{ view.transform = CGAffineTransformScale([self transformForOrientation], 0.001, 0.001); [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:kTransitionDuration/1.5]; [UIView setAnimationDelegate:self]; [UIView setAnimationDidStopSelector:@selector(firstBouncesDidStop)]; view.transform = CGAffineTransformScale([self transformForOrientation], 1.1, 1.1); [UIView commitAnimations];}-(void)dismissAlertAnimation{ [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:kTransitionDuration/2]; [UIView setAnimationDelegate:self]; view.alpha = 0; [UIView setAnimationDidStopSelector:@selector(dismissAlertAnimationDidStoped)]; [UIView commitAnimations];}#pragma mark -- UIViewAnimation delegate-(void)firstBouncesDidStop{ [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:kTransitionDuration/2]; [UIView setAnimationDelegate:self]; [UIView setAnimationDidStopSelector:@selector(secondBouncesDidStop)]; view.transform = CGAffineTransformScale([self transformForOrientation], 0.9, 0.9); [UIView commitAnimations]; }-(void)secondBouncesDidStop{ [UIView beginAnimations:nil context:nil]; [UIView setAnimationDelegate:self]; [UIView setAnimationDuration:kTransitionDuration/2]; view.transform = [self transformForOrientation]; [UIView commitAnimations]; //You can do somethings at the end of animation [self.delegate showCustomizedAlertAnimationIsOverWithUIView:view];}-(void)dismissAlertAnimationDidStoped{ [self.delegate dismissCustomizedAlertAnimationIsOverWithUIView:view];}@end |
//// ;// AlertDemo//// Created by PSH_Chen_Tao on 7/19/13.// Copyright (c) 2013 wolfman. All rights reserved.////自定义的 alert view 类#import <Foundation/Foundation.h>#import "CustomizedAlertAnimation.h"@protocol CustomeAlertViewDelegate ;@interface CustomeAlertView : UIWindow <CustomizedAlertAnimationDelegate>@property(strong,nonatomic)UIView *myView;@property(strong,nonatomic)UIActivityIndicatorView *activityIndicator;@property(strong,nonatomic)CustomizedAlertAnimation *animation;@property(assign,nonatomic)id<CustomeAlertViewDelegate> delegate;-(void)show;@end@protocol CustomeAlertViewDelegate-(void)CustomeAlertViewDismiss:(CustomeAlertView *) alertView;@end |
//// CustomeAlertView.m// AlertDemo//// Created by PSH_Chen_Tao on 7/19/13.// Copyright (c) 2013 wolfman. All rights reserved.//#import "CustomeAlertView.h"@implementation CustomeAlertView@synthesize myView;@synthesize activityIndicator;@synthesize animation;@synthesize delegate;-(id)init{ if (self=[super init]) { self.frame = [[UIScreen mainScreen] bounds]; self.backgroundColor = [UIColor clearColor]; //UIWindow的层级 总共有三种 self.windowLevel = UIWindowLevelAlert; myView = [[UIView alloc]initWithFrame:CGRectMake(30, 140, 260, 200)]; UIButton *okButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [okButton setBackgroundImage:[UIImage imageNamed:@"alert-view-ok-button"] forState:UIControlStateNormal]; [okButton addTarget:self action:@selector(pressoKButton:) forControlEvents:UIControlEventTouchUpInside]; okButton.frame = CGRectMake(90, 130, 80, 40); [myView addSubview:okButton]; // [okButton release]; activityIndicator = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(105, 75, 50, 50)]; activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhite; [myView addSubview:activityIndicator]; // [activityIndicator release]; UIImageView *imageView = [[UIImageView alloc]initWithFrame:myView.bounds]; [imageView setImage:[[UIImage imageNamed:@"alert-view-bg-portrait"] stretchableImageWithLeftCapWidth:100 topCapHeight:30]]; [myView insertSubview:imageView atIndex:0]; [imageView release]; animation = [[CustomizedAlertAnimation alloc]customizedAlertAnimationWithUIview:myView]; animation.delegate = self; [self addSubview:myView]; [myView release]; } return self;}-(void)show{ [self makeKeyAndVisible]; [animation showAlertAnimation];}-(void)dismiss{ [self resignKeyWindow]; [animation dismissAlertAnimation]; }-(void) pressoKButton:(id)sender{ [self dismiss];}#pragma mark -- CustomizedAlertAnimationDelegate//自定义的alert view出现动画结束后调用-(void)showCustomizedAlertAnimationIsOverWithUIView:(UIView *)v{ NSLog(@"showCustomizedAlertAnimationIsOverWithUIView"); [activityIndicator startAnimating];}//自定义的alert view消失动画结束后调用-(void)dismissCustomizedAlertAnimationIsOverWithUIView:(UIView *)v{ NSLog(@"dismissCustomizedAlertAnimationIsOverWithUIView"); [activityIndicator stopAnimating]; [self.delegate CustomeAlertViewDismiss:self]; }@end |
//// ViewController.h// AlertDemo//// Created by PSH_Chen_Tao on 7/19/13.// Copyright (c) 2013 wolfman. All rights reserved.//#import <UIKit/UIKit.h>#import "CustomeAlertView.h"@interface ViewController : UIViewController <UIAlertViewDelegate,CustomeAlertViewDelegate>- (IBAction)showTraditionAlert:(id)sender;- (IBAction)showWindowAlert:(id)sender;@property(strong,nonatomic) UIActivityIndicatorView *indicator;@property(strong,nonatomic)CustomeAlertView *customeAlertView;@end |

//
// ViewController.m
// AlertDemo
//
// Created by PSH_Chen_Tao on 7/19/13.
// Copyright (c) 2013 wolfman. All rights reserved.
// #import "ViewController.h" @interface ViewController () @end @implementation ViewController @synthesize indicator; @synthesize customeAlertView; - (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib. } - (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} - (IBAction)showTraditionAlert:(id)sender { UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"正在下载....." message:nil delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];
alertView.delegate = self;
[alertView show];
} - (IBAction)showWindowAlert:(id)sender { customeAlertView = [[CustomeAlertView alloc]init];
customeAlertView.delegate = self; [customeAlertView show];
} #pragma mark -- UIAlertViewDelegate //实现代理增加网络指示器
- (void)willPresentAlertView:(UIAlertView *)alertView;{
indicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
indicator.frame = CGRectMake(110, 20, 50, 50); [alertView addSubview:indicator];
[indicator startAnimating];
[indicator release];
} - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
[indicator stopAnimating];
} #pragma mark -- CustomeAlertViewDelegate -(void)CustomeAlertViewDismiss:(CustomeAlertView *) alertView{
[alertView release]; NSLog(@"CustomeAlertViewDismiss");
}
@end

对于UIWindow的相关东西可以参考 http://www.cnblogs.com/smileEvday/archive/2012/03/27/2420362.html#2728097
注意设计到UIWindow显示的工程不能用arc ,具体原因还没找到让我信服的 ,希望知道的大牛可以回复下。。。。。。。
IOS 7 自定义的UIAlertView不能在iOS7上正常显示的更多相关文章
- 自定义的UIAlertView不能在iOS7上正常显示
众所周知,当伟大的iOS7系统发布后,表扬的一堆.谩骂的也一片,而对于我们程序员来说最关心的莫过于低版本系统上的程序在搞版本系统上的兼容性问题了. 在iOS6.1几之前,当我们想要做一些提醒用户或临时 ...
- 自定义plain 样式的 tableview,模拟器上不显示分割线,真机上却显示分割线.
一, 经历 1> 自定义plain 样式的 tableview,模拟器上不显示分割线,真机上却显示cell 下面的分割线. 2> 尝试使用表格的separatorStyle属性,尝试失败. ...
- iOS开发-自定义UIAlterView(iOS 7)
App中不可能少了弹框,弹框是交互的必要形式,使用起来也非常简单,不过最近需要自定义一个弹框,虽然iOS本身的弹框已经能满足大部分的需求,但是不可避免还是需要做一些自定义的工作.iOS7之前是可以自定 ...
- ios/mac/COCOA系列 -- UIALertVIew 学习笔记
最近在学习ios开发,学习的书籍<ios7 Pragramming cookbook>,做笔记的目的以后方便查看.笔记形式是小例子,将书上的例子书写完整. UIAlertViewClass ...
- iOS 如何自定义UISearchBar 中textField的高度
iOS 如何自定义UISearchBar 中textField的高度 只需设置下边的方法就可以 [_searchBar setSearchFieldBackgroundImage:[UIImage i ...
- iOS 隐藏自定义tabbar
iOS 隐藏自定义tabbar -(void)viewWillAppear:(BOOL)animated { NSArray *array=self.tabBarController.view.su ...
- ios 实现自定义状态栏StatusBar 和 导航栏navigationBar 的状态和颜色
很多app中可以看到不同与导航栏的状态栏的颜色,他妈的真绕嘴. 一.更改状态栏颜色 (StatusBar) 就是比如导航栏是红色的状态栏是绿色的. 要实现这样的效果其实很简单,就是添加一个背景view ...
- IOS开发UI基础 UIAlertView的属性
UIAlertView1.Title获取或设置UIAlertView上的标题. 2.Message获取或设置UIAlertView上的消息 UIAlertView *alertView = [[UIA ...
- 【转】IOS7 MPMoviePlayerViewController横屏显示
在应用程序中用到MPMoviePlayerViewController时,有时需要保持应用程序为竖屏状态,而视频播放器显示为横屏,如何做呢?如果采用强制横屏的方法,应用审核的时候是不会通过的,因为该方 ...
随机推荐
- php连接sql2005
连接前配置系统: 1.检查文件 php5.2.5/ntwdblib.dll 默认下面有一个,不能连接再替换. 下载正确版本的 ntwdblib.dll (2000.80.194.0),地址: http ...
- FFmpeg音视频同步示例
原文地址:https://my.oschina.net/u/555002/blog/79324 前面整个的一段时间,我们有了一个几乎无用的电影播放器.当然,它能播放视频,也能播放音频,但是它还不能被称 ...
- EMQ ---v2.3.11源码成熟度
从原作者那边了解到,总体还可以,但是做不到99.99%稳定.主要是连接内存占用没有保护. pubsub均衡时很稳定,但是集群或大量消息向少量订阅发布时会崩溃,小概率情况. EMQ中CPU是公平分配给M ...
- 实现简单容器模板类Vec(vector capacity 增长问题、allocator 内存分配器)
首先,vector 在VC 2008 中的实现比较复杂,虽然vector 的声明跟VC6.0 是一致的,如下: C++ Code 1 2 template < class _Ty, cl ...
- 安卓编译报错:Dex Loader] Unable to execute dex: java.nio.BufferOverflowException. Check the Eclipse log for stack trace.
今天在编译别人的project时遇到了这个错误,心想应该不是代码的问题.网上搜了一下,应该是sdk版本设置的问题,要设置为最新sdk版本才可以. 解决办法就是修改project.properties里 ...
- TCP/UDP,SOCKET,HTTP,FTP 简析
(一)TCP/UDP,SOCKET,HTTP,FTP简析 TCP/IP是个协议组,可分为三个层次:网络层.传输层和应用层: 网络层:IP协议.ICMP协议.ARP协议.RARP协议和BOOTP协议 传 ...
- centos Permission denied: make_sock: could not bind to address
CentOS 下启动Httpd 失败,报 (13)Permission denied: make_sock: could not bind to address [::]:8000 因为 小于1024 ...
- linux下利用shell脚本和mysqldump热备份和恢复mysql
对mifeng数据库进行每周六3:33完全热备份,并可以完全恢复! 一.先建立备份脚本 #vi /mifengbackup/backup.sh #!bin/bash cd /mifengbackup ...
- quick cocos2d-x 2.2.4 window环境调试
BabeLua简介 BabeLua是一款基于VS2012/2013(简称VS)的免费开源的Lua集成开发环境,在Lua编辑和调试方面,具有如下功能和特性: ●Lua语法高亮 ●语法检查 ●自动补全 ● ...
- Python字符与ASCII码转换
有两个内置函数,记得以前在<Python Cookbook>里看到过. >>>print ord('a') 97 >>>print chr(97) a