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

Ios 如何以编程方式设置设备(UI)方向?,ios,iphone,objective-c,orientation,Ios,Iphone,Objective C,Orientation,希望屏幕(UI)上的所有内容都能够从横向向左向右旋转,或从横向向右旋转 我该怎么做呢?这是私人的吗 我知道 -(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {} 您可以说UI可以是哪个方向,但是有没有一种方法一次只能强制一个方向 Cheers调用该方法以确定您的界面是否应自动旋转到给定的旋转(即让UIKit完成繁重的工作,而不是您手动完成) 因此,如果您希望您的

希望屏幕(UI)上的所有内容都能够从横向向左向右旋转,或从横向向右旋转

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

我知道

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {}
您可以说UI可以是哪个方向,但是有没有一种方法一次只能强制一个方向


Cheers

调用该方法以确定您的界面是否应自动旋转到给定的旋转(即让UIKit完成繁重的工作,而不是您手动完成)

因此,如果您希望您的应用程序仅在横向环境中工作,您可以通过以下方式实现该方法的主体:


返回UIInterfaceOrientationIsLandscape(interfaceOrientation);

如果您希望UI自动旋转到所有方向,您可以

返回YES;


这就是您要问的吗?

如果您提供了一个模态视图控制器,它实现了
-shouldAutorotateToInterfaceOrientation:
只支持一个方向,整个界面将自动强制进入其中。我不认为有其他方法可以通过编程改变方向。

在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];
    }

}

我在这方面取得了成功:

 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];
}

同样,这种简单的方法也适用于:

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

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

这是由:Guntis Treulands解决的


在Swift中将方向更改为纵向:

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

请参阅:

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

- (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);
}

As
shouldAutorotateToInterfaceOrientation
不推荐使用。较新的应用程序应采用以下方法,如Apple的中所述,直到它们自己被弃用为止:

  • 应自动旋转
  • 首选交互方向进行演示
    ;及
  • 尝试旋转到设备定向


据我所知,对于需要锁定(即具有首选)方向模式(与上次不同)的视图,最好使用不同的视图控制器。

否,而是在程序中的不同时间通过代码将UI方向设置为不同的方向。(因此,对于某些上下文,它可以镜像另一个设备)哦,好吧,在这种情况下,我可能会对VC在该委托方法中支持的方向返回YES,然后尝试使用:[[UIDevice currentDevice]setOrientation:UIInterfaceOrientationAndscapeRight]强制方向;我不知道我是否曾亲自尝试过这样做,但可能会奏效……不确定这是否是您的问题,但将您的shouldAutorotateToInterfaceOrientation方法更改为:return UIInterfaceOrientationIsLandscape(interfaceOrientation);然后可能会按照贾斯汀·斯帕尔·萨默斯的话做,并以模态的方式呈现视图控制器。这样,您可能不必使用setOrientation强制方向。做疯狂的定向可能是个婊子…@half_brick:所以我照你说的做了,效果很好。我的nib文件处于横向模式。特定的景观总是在风景中。但是,如果我在横向旋转硬件(另一种方式),视图控制器不会旋转。。。有什么帮助吗?谢谢!我只需要UIView.transform属性,因为我正在处理的视图只需要横向。CoreMotion方向呢?它是否随状态栏方向而改变?不,它不随状态栏方向而改变。CoreMotion发送设备运动事件此代码无法更改设备方向它只更改了应用程序屏幕方向。我不知道如何将
UIDeviceOrientation
用于
setStatusBarOrientation
。根据,您需要使用
UIInterfaceOrientation
。但也许你能解释一下你的理由?首先谢谢你Chis!!但为什么苹果让这种方法(setOrientation:)不适用于我们?这很简单。但它也是苹果proove吗?ThxBy使用此方法是否有可能在苹果审批过程中被拒绝?上述代码将不会在ARC下编译。它不喜欢从NSInteger到id的转换。但是,您可以改用NSInvocation。也就是说,它在iOS 6下对我不起作用。您可以使用以下方法克服ARC问题:
[[UIDevice currentDevice]performSelector:NSSelector-FromString(@“setOrientation:”)with object:(u桥id)((void*)UIInterfaceOrientationGrait)]但它在iOS6下无论如何都无法工作。这帮助我解决了IPAD发布时遇到的问题:根本没有收到任何触摸事件。调用setStatusBarHidden神奇地解决了这个问题。
shouldAutorotateToInterfaceOrientation
在iOS 6.0中被弃用。请看。强制人像代码,对于我的应用程序,它的工作可能重复谢谢你,我需要这个答案!
UIDevice.currentDevice().setValue(UIInterfaceOrientation.Portrait.rawValue, forKey: "orientation")
- (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);
}