如何以编程方式设置设备(UI)方向?

时间:2010-12-02 01:56:16

标签: ios iphone objective-c orientation

希望屏幕(UI)上的所有内容都能够从左到右旋转,反之亦然。

我该怎么做呢?这是私人的吗?

我知道

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

让你说出用户界面可以有哪些方向,但有没有办法一次只强制一个?

干杯

10 个答案:

答案 0 :(得分:24)

在iOS [[UIDevice currentDevice] setDeviceOrientation:UIDeviceOrientationSomeOrientation]中,方法不存在。但我们可以使用状态栏旋转视图,为此:

- (void)showAlbum {
    // check current orientation
    if ([[UIApplication sharedApplication] statusBarOrientation] != UIInterfaceOrientationLandscapeLeft) {
        // no, the orientation is wrong, we must rotate the UI
        self.navigationController.view.userInteractionEnabled = NO;
        [UIView beginAnimations:@"newAlbum" context:NULL];
        [UIView setAnimationDelegate:self];
        // when rotation is done, we can add new views, because UI orientation is OK
        [UIView setAnimationDidStopSelector:@selector(addAlbum)];
        // setup status bar
        [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft  animated:NO];
        // rotate main view, in this sample the view of navigation controller is the root view in main window
        [self.navigationController.view setTransform: CGAffineTransformMakeRotation(M_PI / 2)];
        // set size of view
        [self.navigationController.view setFrame:CGRectMake(0, 0, 748, 1024)];
        [UIView commitAnimations];
    } else {
        [self addAlbum];
    }

}

答案 1 :(得分:18)

我在这方面取得了成功:

 if (self.interfaceOrientation != UIInterfaceOrientationPortrait) {
    // http://stackoverflow.com/questions/181780/is-there-a-documented-way-to-set-the-iphone-orientation
    // http://openradar.appspot.com/radar?id=697
    // [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationPortrait]; // Using the following code to get around apple's static analysis...
    [[UIDevice currentDevice] performSelector:NSSelectorFromString(@"setOrientation:") withObject:(id)UIInterfaceOrientationPortrait];
}

答案 2 :(得分:16)

调用该方法以确定您的界面是否应自动旋转到给定的旋转(即让UIKit执行艰苦的工作,而不是手动执行)。

因此,如果您希望自己的应用仅在横向工作,则可以使用以下方法实现该方法的主体:

return UIInterfaceOrientationIsLandscape(interfaceOrientation);

如果您希望自动界面可以自动旋转到所有方向 return YES;

这就是你问的问题吗?

答案 3 :(得分:5)

如果你提出一个实现-shouldAutorotateToInterfaceOrientation:的模态视图控制器只支持一个方向,整个界面将自动强制进入它。我不认为有办法以编程方式改变方向。

答案 4 :(得分:3)

这种简单的方法也适用:

[[UIApplication sharedApplication] setStatusBarHidden:YES];
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];

并返回:

[[UIApplication sharedApplication] setStatusBarHidden:NO];
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait];

答案 5 :(得分:3)

在Swift中将方向更改为肖像:

UIDevice.currentDevice().setValue(UIInterfaceOrientation.Portrait.rawValue, forKey: "orientation")

请参阅:https://stackoverflow.com/a/24259601

答案 6 :(得分:2)

这是通过以下方式解决的:Guntis Treulands https://stackoverflow.com/a/10146270/894671

//-----------------------------------
// FORCE PORTRAIT CODE
//-----------------------------------
[[UIApplication sharedApplication] setStatusBarOrientation:UIDeviceOrientationPortrait animated:NO];
//present/dismiss viewcontroller in order to activate rotating.
UIViewController *mVC = [[UIViewController alloc] init];
[self presentModalViewController:mVC animated:NO];
[self dismissModalViewControllerAnimated:NO];

答案 7 :(得分:0)

if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
        objc_msgSend([UIDevice currentDevice], @selector(setOrientation:),    UIInterfaceOrientationLandscapeLeft );
    }

答案 8 :(得分:0)

尽管这不是一个闲置的解决方案,但对我来说效果很好。

- (void)viewDidLoad
{
   self.view.transform = CGAffineTransformIdentity;
   self.view.transform = CGAffineTransformMakeRotation((M_PI * (90) / 180.0)); 
   self.view.bounds = CGRectMake(0.0, 0.0, 480, 320);
}

答案 9 :(得分:0)

不推荐使用suggested on a related thread shouldAutorotateToInterfaceOrientation。较新的应用程序应该使用Apple的UIViewController Class Reference中描述的以下方法,直到它们自己被弃用为止:

  • shouldAutorotate
  • preferredInterfaceOrientationForPresentation;和
  • attemptRotationToDeviceOrientation

根据我的理解,最好使用不同的视图控制器来查看需要锁定(即具有首选)方向模式的视图,该模式与上一个不同。