![](/img/trans.png)
[英]How to lock orientation for iPhone for certain view controllers - Swift?
[英]Disable orientation in certain View Controllers if device is iPhone
如果滿足以下條件,我試圖使我的應用程序禁用橫向方向:
WelcomeViewController
, LogInViewController
,或SignUpViewController
我已經在AppDelegate.m
嘗試過:
- (NSUInteger) application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
if ( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone ) {
if ([[window.rootViewController presentedViewController] isKindOfClass:[WelcomeViewController class]])
return UIInterfaceOrientationMaskPortrait;
else
return UIInterfaceOrientationMaskAllButUpsideDown;
}
}
由於出現錯誤,說Control may reach end of-non-void function
所以這不起作用。
我也試過這個:
- (NSUInteger) application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
NSString *deviceModel = (NSString*)[UIDevice currentDevice].model;
if ([deviceModel rangeOfString:@"iPhone"].location != NSNotFound && [[window.rootViewController presentedViewController] isKindOfClass:[WelcomeViewController class]]) {
NSLog(@"I am an iPhone");
return UIInterfaceOrientationMaskPortrait;
} else if ([deviceModel rangeOfString:@"iPhone"].location != NSNotFound && [[window.rootViewController presentedViewController] isKindOfClass:[LogInViewController class]]) {
NSLog(@"I am an iPhone");
return UIInterfaceOrientationMaskPortrait;
} else if ([deviceModel rangeOfString:@"iPhone"].location != NSNotFound && [[window.rootViewController presentedViewController] isKindOfClass:[SignUpViewController class]]) {
NSLog(@"I am an iPhone");
return UIInterfaceOrientationMaskPortrait;
} else {
NSLog(@"I am NOT an iPhone");
return UIInterfaceOrientationMaskAllButUpsideDown;
}
}
但是,如果在iPhone上進行測試,它將直接進入else語句,這很有趣,因為如果刪除[[window.rootViewController presentedViewController] isKindOfClass:[SignUpViewController class]]
,它將可以正常工作。
我究竟做錯了什么?
由於出現錯誤,說Control可能到達非空函數的結尾,所以這不起作用。
好吧,請考慮一下您的邏輯(或缺乏邏輯):
- (NSUInteger) application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
if ( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad ) {
if ([[window.rootViewController presentedViewController] isKindOfClass:[WelcomeViewController class]])
return UIInterfaceOrientationMaskPortrait;
else
return UIInterfaceOrientationMaskAllButUpsideDown;
}
}
如果UI_USER_INTERFACE_IDIOM()
是UIUserInterfaceIdiomPad
,則在每種情況下( if
和else
)都將返回某些else
。 但是否則,您不會說該怎么辦! 如果UI_USER_INTERFACE_IDIOM()
不是 UIUserInterfaceIdiomPad
怎么UIUserInterfaceIdiomPad
? 那你還回來什么? 沒有! 因此,這沒有任何意義。 您提供的信息不足。 您必須涵蓋所有可能的情況, 無論可能發生什么 ,都要返回。
就個人而言,我認為您進行此操作的整個過程都非常愚蠢。 我要做的是在WelcomeViewController,LogInViewController和SignUpViewController這三個類中的每一個中實現supportedInterfaceOrientations
。 然后,在每種實現方式中,我都會一開始就准確地表達您的意思:如果設備是iPad,則返回任何方向,但是如果設備是iPhone,則返回縱向。 是的,這涉及到一些重復-在所有三個視圖控制器中, supportedInterfaceOrientations
的實現都是相同的-但是誰在乎呢? 至少它將是可以理解的-並且它將起作用,這比您現在想要的要多。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.