2014-02-25 34 views
3

我正在iOS中开发应用程序。我需要通过点击按钮锁定或解锁方向。锁定/解锁iOS中的方向更改

我已选中this链接。

我需要锁定单击按钮上的屏幕,如果再次点击意味着需要解锁。

我已经尝试使用此代码,但没有用。

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


-(NSUInteger)supportedInterfaceOrientations 
{ 
    return UIInterfaceOrientationPortrait | UIInterfaceOrientationLandscapeLeft | UIInterfaceOrientationLandscapeRight |UIInterfaceOrientationPortraitUpsideDown; 
} 

任何帮助表示赞赏。

+0

我不知道苹果公司已经修复了它,但是应该在iOS 5中完成自动旋转到接口的定向工作,并且从IOS6开始完全没有用处。我最终修复了整个应用程序的方向,因为我无法再一页一页地控制它。我很乐意知道这是否已经解决。 – Paulo

+1

@ paulo它不是不工作,从ios 6起支持使用supportedInterfaceOrientations。 – Pochi

+0

从iOS 6开始,方向更改将在容器控制器(如导航控制器或选项卡栏控制器)上继续。您可以重写容器控制器方法来逐页控制方向更改。 [请参阅此处](http://stackoverflow.com/questions/15004271/prevent-autorotate-for-one-view-controller/15004471#15004471) –

回答

2

如果你想只锁定按钮点击旋转,那么你可以使用。

- (BOOL)shouldAutorotate 
{ 
    if (autorotate) { 
    return YES; 
    } 
    else 
    { 
    return NO; 
    } 
} 

因为shouldAutorotateToInterfaceOrientation现在是DEPRECATED。

使用此方向将锁定当前方向。

+0

并不意味着它不应该实现,这意味着如果他计划只支持ios 6+然后不需要,否则他必须实现shouldAutorotateToInterfaceOrientation来支持以前的ios版本。 – Pochi

+0

@Chiquis是的,你是对的。但如果你想支持ios6 +,那么它不适合你。还有一个问题,ios 6具有ios 6方向不支持导航,在这种情况下,您必须覆盖方向。 –

+0

我的意思是如果你想支持最广泛的iOS版本,你应该实现两者。我不明白关于iOS 6不支持导航的部分。 – Pochi

0

我有发现一些interesant,你可以做到这一点,锁定旋转

[的UIDevice currentDevice] endGeneratingDeviceOrientationNotifications]。

,然后再次启用

[[的UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

很容易

测试真实iPhone设备7.1.1,美国商务部说,这是因为可用的iOS 2.0因此必须在所有设备上工作
编号: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIDevice_Class/Reference/UIDevice.html#//apple_ref/occ/instm/UIDevice/endGeneratingDeviceOrientationNotifications