# 1. 全局禁止橫屏
在Appdelegate.h添加以下屬性:
/*** 是否允許橫屏的標記 */
@property (nonatomic,assign)BOOL allowRotation;
Appdelegate.m添加如下代碼:
-(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
if (self.allowRotation) {//如果設(shè)置了allowRotation屬性,支持全屏
return UIInterfaceOrientationMaskAll;
}
return UIInterfaceOrientationMaskPortrait;//默認全局不支持橫屏
}
2. 在需要支持橫屏的界面改變allowRotation屬性
//進入全屏
-(void)begainFullScreen
{
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.allowRotation = YES;
}
// 退出全屏
-(void)endFullScreen
{
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.allowRotation = NO;
//強制歸正:
if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
SEL selector = NSSelectorFromString(@"setOrientation:");
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
[invocation setSelector:selector];
[invocation setTarget:[UIDevice currentDevice]];
int val =UIInterfaceOrientationPortrait;
[invocation setArgument:&val atIndex:2];
[invocation invoke];
}
}
在viewWillAppear和viewWillDisappear分別調(diào)用以上方法,在該控制器下即可順利支持全屏。
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self begainFullScreen];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[self endFullScreen];
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。