通讀iOS 屏幕旋轉(zhuǎn)的實(shí)踐解析可以知道, 設(shè)置屏幕旋轉(zhuǎn)的全局權(quán)限主要有兩種方式, 且代碼設(shè)置的優(yōu)先級(jí)高于Device Orientation 屬性配置;
因此通過代碼在Appdelegate中設(shè)置屏幕旋轉(zhuǎn)的全局權(quán)限, 然后在需要使用的地方來設(shè)置是否支持屏幕旋轉(zhuǎn)即可:
一、代碼設(shè)置屏幕旋轉(zhuǎn)的全局權(quán)限 (有注釋)
Appdelegate.h
@interface AppDelegate : UIResponder <UIApplicationDelegate>
/** iPhone 是否支持轉(zhuǎn)屏 */
@property (nonatomic, assign, getter=isAllowRotate) BOOL allowRotate;
/** iPad 是否支持轉(zhuǎn)屏 */
@property (nonatomic, assign, getter=isIPadAllowRotate) BOOL iPadAllowRotate;
/** 是否不支持豎屏, 一般用于視頻的全屏播放 */
@property (nonatomic, assign, getter=isNonSupportPortraitOrientations) BOOL nonSupportPortraitOrientations;
- (void)resetAllowRotate:(NSNumber *)theAllowRotate;
- (void)resetIPadAllowRotate:(NSNumber *)theAllowRotate;
- (void)resetNonSupportPortraitOrientations:(NSNumber *)isNonSupport;
@end
Appdelegate.m
#pragma mark - Orientations
- (void)resetAllowRotate:(NSNumber *)theAllowRotate {
_allowRotate = [theAllowRotate boolValue];
}
- (void)resetNonSupportPortraitOrientations:(NSNumber *)isNonSupport {
_nonSupportPortraitOrientations = [isNonSupport boolValue];
}
- (void)resetIPadAllowRotate:(NSNumber *)theAllowRotate {
[self resetAllowRotate:theAllowRotate];
_iPadAllowRotate = [theAllowRotate boolValue];
}
// 返回需要支持的方向
// 如果我們實(shí)現(xiàn)了Appdelegate的這一方法,那么我們的App的全局旋轉(zhuǎn)設(shè)置將以這里的為準(zhǔn)
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
if (self.isAllowRotate) {
if (self.isNonSupportPortraitOrientations) {
return UIInterfaceOrientationMaskLandscape;
} else {
return UIInterfaceOrientationMaskAllButUpsideDown;
}
} else {
return UIInterfaceOrientationMaskPortrait;
}
}
當(dāng)然, 示例代碼表示在未進(jìn)行特殊處理的情況下, 僅支持UIInterfaceOrientationMaskPortrait, 也就是豎屏顯示. 按照想所需的業(yè)務(wù)場景可自行配置。
另外, 當(dāng)系統(tǒng)屏幕旋轉(zhuǎn)開關(guān)鎖定時(shí), 不論supportedInterfaceOrientationsForWindow支持那些方向, 項(xiàng)目都不會(huì)跟隨手機(jī)感應(yīng)器旋轉(zhuǎn)。
二、需要的位置設(shè)置是否支持橫豎屏幕
以Appdelegate.m示例代碼為例, 想進(jìn)入一個(gè)橫屏的視頻播放界面, 就需要在進(jìn)入之前設(shè)置屏幕支持旋轉(zhuǎn), 退出視頻之后再取消支持屏幕旋轉(zhuǎn)。
這里是權(quán)限設(shè)置的一個(gè)封裝方法:
- (void)setResetAllowRotate:(BOOL)allowRotate {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wundeclared-selector"
if ([[UIApplication sharedApplication].delegate respondsToSelector:@selector(resetAllowRotate:)]) {
[[UIApplication sharedApplication].delegate performSelector:@selector(resetAllowRotate:) withObject:[NSNumber numberWithBool:allowRotate]];
}
#pragma clang diagnostic pop
}
2.1 在進(jìn)入橫屏頁面之前調(diào)用:
[self setResetAllowRotate:YES];
2.2 在退出橫屏頁面之后調(diào)用:
[self setResetAllowRotate:NO];
- 針對(duì)預(yù)處理指令的意義有不理解的, 可以參考iOS #pragma & #pragma clang diagnostic
三、代碼在運(yùn)行iPad上過程中, supportedInterfaceOrientationsForWindow 不調(diào)用的問題.
fix: 打開Device Orientation 屬性配置, 勾選Requires full screen選項(xiàng)即可。
Device Orientation 屬性配置:
項(xiàng)目 -> TARGET -> General -> Deployment Info
默認(rèn)的勾選配置
開啟Requires full screen
-
Requires full screen 勾選, 代表著iPad 需要全屏幕展示, 也就代表著
存在橫豎屏幕的情況, window尺寸無非是橫豎屏幕變換。 -
Requires full screen 未勾選, 代表著iPad 不需要全屏幕展示, 也就代表著
不存在橫豎屏幕的情況, window尺寸可以隨便變換,只有寬和高的定義, 一般用來支持iPad項(xiàng)目分屏處理。
結(jié)語
路漫漫其修遠(yuǎn)兮,吾將上下而求索~
.End

