iOS - 強制旋轉屏幕之代碼實現

最近接手了一個項目,正常情況下使用查看圖片是沒問題的。

用到了 MWPhotoBrowser 這個第三方圖片瀏覽庫。

不過發現了一個問題,就是設備橫屏modalMWPhotoBrowser的時候,發生了圖片位置錯亂。

實在沒辦法,所以想到了一個餿主意。

就是modal的時候使用代碼把設備強制旋轉回去。

//UIDevice+WJ.h
@interface UIDevice (WJ)
/**
 *  強制旋轉設備
 *  @param  旋轉方向
 */
+ (void)setOrientation:(UIInterfaceOrientation)orientation;
@end
//UIDevice+WJ.m
#import "UIDevice+WJ.h"
@implementation UIDevice (WJ)
//調用私有方法實現
+ (void)setOrientation:(UIInterfaceOrientation)orientation {
    SEL selector = NSSelectorFromString(@"setOrientation:");
    NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[self instanceMethodSignatureForSelector:selector]];
    [invocation setSelector:selector];
    [invocation setTarget:[self currentDevice]];
    int val = orientation;
    [invocation setArgument:&val atIndex:2];
    [invocation invoke];
}
@end

參考:

非常感謝我的朋友 pypy ,根據他的博客解決了我現在的問題。
http://pypy.me/ios-heng-shu-ping-de-liang-chong-shi-xian-fang-an/
根據他所提供的方法簡單的封裝了一個分類。

總結:

不管什么主意,能解決現在的問題都是好主意。
就算是餿主意也好,根據現狀把問題解決了,后續才考慮優化。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容