iOS 7 下 UIAlertView 強制橫屏實現

分類:編程 時間:2017-02-15

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


Tags: 而且 項目 新建

文章來源:


ads
ads

相關文章
ads

相關文章

ad