0

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

我试着用

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

还有这个

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

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

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

4

6 回答 6

3

试试这个:

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

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

于 2013-02-13T10:24:55.373 回答
0

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

于 2013-02-13T10:26:41.330 回答
0

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

并将项目设置为横向(右主页按钮)

于 2013-02-13T10:27:24.777 回答
0

如果您只想为 1 个视图控制器执行此操作,您可以通过以下方式执行此操作

- (NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskPortrait;
}

或通过

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

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

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

于 2013-02-13T10:29:06.600 回答
0

您只需在.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
于 2013-02-13T10:30:34.077 回答
0

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

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationMaskPortrait;
}

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

于 2013-02-13T11:14:00.177 回答