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度。