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

时间:2013-02-13 10:21:53

标签: iphone ios objective-c

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

我尝试使用

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

和这个

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

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

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

6 个答案:

答案 0 :(得分:3)

试试这个:

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

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

答案 1 :(得分:0)

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

答案 2 :(得分:0)

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

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

答案 3 :(得分:0)

如果您只想为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

答案 4 :(得分: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

答案 5 :(得分:0)

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

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationMaskPortrait;
}

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

相关问题