在 APPDelegate.h 文件中增加屬性:是否支持橫屏
/*** 是否允許橫屏的標(biāo)記 */
@property (nonatomic,assign)BOOL allowRotation;
在 APPDelegate.m 文件中增加方法,控制全部不支持橫屏
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
if (self.allowRotation) {
return UIInterfaceOrientationMaskAllButUpsideDown;
}
return UIInterfaceOrientationMaskPortrait;
}
這樣在其他界面想要橫屏的時(shí)候,我們只要控制 allowRotation 這個(gè)屬性就可以控制其他界面進(jìn)行橫屏了。
//需在上面#import "AppDelegate.h"
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.allowRotation = YES;
//不讓橫屏的時(shí)候 appDelegate.allowRotation = NO;即可
需要橫屏?xí)r,進(jìn)入界面時(shí)
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
//如果有導(dǎo)航設(shè)置的話 先設(shè)置導(dǎo)航
[self.navigationController setNavigationBarHidden:YES animated:YES];
[self begainFullScreen];
}
-(void)begainFullScreen
{
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.allowRotation = YES;
}
需要橫屏?xí)r,退出界面時(shí)
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
//如果有導(dǎo)航設(shè)置的話 先設(shè)置導(dǎo)航
//[self.navigationController setNavigationBarHidden:NO animated:YES];
//不讓橫屏的時(shí)候
[self endFullScreen];
}
// 退出全屏
-(void)endFullScreen
{
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.allowRotation = NO;
//強(qiáng)制歸正:
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];
}
}
界面橫屏 所以這里可以使用 通知,就可以解決
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(begainFullScreen) name:UIWindowDidBecomeVisibleNotification object:nil];//進(jìn)入全屏
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(endFullScreen) name:UIWindowDidBecomeHiddenNotification object:nil];//退出全屏
在退出全屏?xí)r,增加邏輯讓其強(qiáng)制編程豎屏,這樣當(dāng)退出時(shí)就會(huì)自動(dòng)變成豎屏了。
```
```
// 進(jìn)入全屏
-(void)begainFullScreen
{
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.allowRotation = YES;
}
```
```
// 退出全屏
-(void)endFullScreen
{
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.allowRotation = NO;
//強(qiáng)制歸正:
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];
}
}
```
一進(jìn)入頁(yè)面就橫屏:
```
NSNumber *orientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft];
[[UIDevice currentDevice] setValue:orientationTarget forKey:@"orientation"];
```
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。