繁体   English   中英

如何锁定设备上更改方向?

[英]How to lock changing orientation on device?

如何锁定设备上更改方向?

我尝试使用

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

和这个

- (void)viewDidAppear:(BOOL)animated
{
    [self shouldAutorotateToInterfaceOrientation: UIInterfaceOrientationLandscapeRight];
    [super viewDidAppear:animated];
}

但是当我使用设备时没有任何效果。 当应用在模拟器上运行时,效果很好。

模拟器具有iOS 6和设备5.1.1。 我做错了什么?

尝试这个:

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

该方法返回“是”或“否”,并回答问题“我应该旋转到该方向:interfaceOrientation”。 因此,您应该返回YES或NO- UIInterfaceOrientationLandscapeRight不是BOOL。 但是您可以使用比较来返回布尔值,如上所述。

打开您的项目,选择项目文件,转到“摘要”部分,然后在“支持的界面方向”子部分中设置所需的界面方向。 祝您好运!(适用于iOS 6.x)

在您的项目.plist文件中,添加支持的界面方向

并将项目设置为Landscape(主页按钮)

如果您只想对1个ViewController进行操作,则可以通过以下任一方式进行操作

- (NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskPortrait;
}

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

- (BOOL)shouldAutorotate {
return self.interfaceOrientation == UIInterfaceOrientationPortrait;
}

如果要为整个应用程序设置它,请确保更改Info.plist文件并添加Supported Interface Orientations

您只需在.m文件中添加以下行

#define IOS_OLDER_THAN_6 ( [ [ [ UIDevice currentDevice ] systemVersion ] floatValue ] < 6.0 )
#define IOS_NEWER_OR_EQUAL_TO_6 ( [ [ [ UIDevice currentDevice ] systemVersion ] floatValue ] >= 6.0 )

然后,您必须在.m文件中添加以下方法

#ifdef IOS_OLDER_THAN_6

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
   return (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
#endif

#ifdef IOS_NEWER_OR_EQUAL_TO_6

- (NSUInteger)supportedInterfaceOrientations
{
   return UIInterfaceOrientationMaskLandscapeRight;
}
#endif

对于iOS 6支持包括以下两种方法

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationMaskPortrait;
}

还请注意,这将适用于xCode 4.5 +

暂无
暂无

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

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