2012-09-05 19 views
2

我有一个在TabBarController中有3个选项卡的应用程序。在第一个选项卡上,当用户在NavigationBar上按下按钮时,可以从纵向旋转到横向?用户第一次无法旋转,只有当他按下按钮时才能旋转。如何启用/禁用方向(旋转),直到按UIBarButtonItem?

我使用SDK iOS 6.

在此先感谢!

+0

谁的轮转ru在说什么? –

+0

@太子对不起,从肖像到风景。 –

回答

5
-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 

    // Return YES for supported orientations 
    return allowedToRotate; 

} 

这时正好有一个BOOL allowedToRotate;在你的类的头,当你想允许旋转它设置为YES。如果您在iOS 6:

-(NSUInteger)supportedInterfaceOrientations { 

    if (allowedToRotate) 
     return UIInterfaceOrientationMaskAll; 
    else 
     return UIInterfaceOrientationMaskPortrait; 

} 

这应该工作,虽然我还没有尝试过...

+0

XCode表示在iOS 6上不推荐使用'shouldAutorotateToInterfaceOrientation:'。但是,我尝试了该代码,但无效。我设置了“返回NO”,我仍然旋转到风景。 –

+0

O是的iOS 6改变了事情,我已经更新了答案......(我没有真正尝试过......) – jjv360

+0

在我的情况下,方法supportedInterfaceOrientations被正确调用,它返回UIInterfaceOrientationMaskPortrait。但是,当我旋转设备时,用户界面也会旋转。为什么? – aneuryzm

2

这奏效了,我......不知道,如果它的最好方式.. ..但它的工作原理:

-(BOOL) shouldAutorotate { 

// Return YES for supported orientations 
return NO; 

} 

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation { 
//Setting the orientation of the view. Ive set it to portrait here. 

return UIInterfaceOrientationPortrait; 

} 
+0

这也适用于我。 thnx :) – deltaaruna

+0

你只是把这些方法覆盖在CustomControllerView.m文件中?不适合我... –