![](/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.