iOS 7 下 UIAlertView 强制横屏实现

来源:互联网 发布:js模拟进度条 编辑:程序博客网 时间:2024/05/21 00:48

iOS 7以后,使用了 UIAlertController 定制性好多了, 但一些需要兼容iOS 7的老项目有一些变态需求还必须使用UIAlertView,而且还不是常规的用法,博主前几日入坑,今天终于跳出坑外呼吸一口新鲜空气,赶紧记下方法来,支援后来者:

UIAlertView 是通常新建一个 UIWindow 来实现的,它有一个代理方法:
-(void)willPresentAlertView:(UIAlertView *)alertView,UIAlertView 在使用了show之后会创建一个新的UIWindow, 并被设置为App的keyWindow, 这里使用视图的transform属性实现竖屏状态下的UIAlertView强制横屏,如下:

-(void)willPresentAlertView:(UIAlertView *)alertView{    GAffineTransform transform = M_PI_2;    [[UIApplication sharedApplication].keyWindow.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop)     {            obj.transform = transform;    }];}

通过设置transform为不同的旋转角度可实现iOS 7下UIAlertView的强制横屏,这里设置旋转90度。

0 0