繁体   English   中英

如何锁定iOS中的屏幕,使其只有纵向方向?

[英]How lock screen in iOS for it to have only portrait orientation?

我创建了一个包含许多视图的应用程序,我想只在纵向方向上安装一些应用程序。 我在.m文件中编写了这个代码:

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return NO;
}

我还需要做点什么吗? 可能在.h文件中?

对我来说最干净的解决方案:

转到Info.plist文件,对于“支持的界面方向”,删除除“纵向(底部主页按钮)”之外的所有值。

您只需要在该方法中返回BOOL。 如果你只想要肖像模式,那意味着:

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return (interfaceOrientation == UIInterfaceOrientationPortrait) ;
}

如果将肖像上下颠倒(当纵向旋转设备180度时),则该方法将如下所示:

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return (interfaceOrientation == UIInterfaceOrientationPortrait) || (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown);
}

最后一个条件可以替换为对UIDeviceOrientationIsPortrait(interfaceOrientation)的调用,它进行相同的比较(参见: http//developer.apple.com/library/ios/#documentation/uikit/reference/UIKitFunctionReference/Reference/reference。 HTML

LE:如果这不起作用,您可以尝试使用以下'hack':尝试弹出并再次推送视图(如果您使用的是NavigationController)。 您可以使用popViewControllerAnimatedpushViewController:animated:方法强制控制器重新查询所需的方向:)来源: http//developer.apple.com/library/ios/#documentation/uikit/reference/UINavigationController_Class/Reference/ Reference.html

你必须返回YES为你支持的方向(纵向),而不是简单NO的一切。 还要确保在项目的目标设置中,您只能将纵向模式检查为支持。

将以下方法添加到viewcontroller。 这将确保仅支持例如portarait模式。

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return UIInterfaceOrientationIsPortrait(interfaceOrientation);
}
-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

尝试这个..

 -(BOOL)shouldAutorotate
 {
  return NO;
 }

-(NSUInteger)supportedInterfaceOrientations
 {
 return UIInterfaceOrientationMaskPortrait;
 }

 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
 {
  return (interfaceOrientation == UIInterfaceOrientationPortrait);
 }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM