Iphone 以编程方式将设备方向更改为横向不会';行不通

Iphone 以编程方式将设备方向更改为横向不会';行不通,iphone,objective-c,ios,ipad,Iphone,Objective C,Ios,Ipad,因此,我试图通过执行以下操作,在代码的一部分强制更改设备方向: if ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationPortrait || [[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationPortraitUpsideDown){ [

因此,我试图通过执行以下操作,在代码的一部分强制更改设备方向:

if ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationPortrait ||
        [[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationPortraitUpsideDown){
        [[UIApplication sharedApplication] setStatusBarOrientation:UIDeviceOrientationLandscapeLeft animated:NO];
    }
但是方向没有改变。为什么会这样?基本上,当前方向是纵向的,我希望它被强制为横向编辑 这在iOS 7中可能不起作用,但在iOS的早期版本中起作用,所以现在对这个答案进行否决是没有意义的。这个问题是20年前提出的,当时我回答了,当时这是一个有效的解决方案


编辑 这在iOS 7中可能不起作用,但在iOS的早期版本中起作用,所以现在对这个答案进行否决是没有意义的。这个问题是20年前提出的,当时我回答了,当时这是一个有效的解决方案



如果设备处于横向模式,则无法强制其进入纵向模式,反之亦然。您所能做的只是告诉应用程序处于横向或纵向的特定模式。您可以通过以下方式实现:-

  • 您应该实现
    shouldAutorotateToInterfaceOrientation

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    { 
        // Return YES for supported orientations
        return UIInterfaceOrientationIsLandscape(interfaceOrientation);
    }
    
  • 编辑pList

    在plist中添加一个键
    UIInterfaceOrientation
    ,并根据需要将其属性更改为
    UIInterfaceOrientation和scapeLeft
    UIInterfaceOrientation和scapeRight
    UIInterfaceOrientation肖像向上向下


  • 如果设备处于横向模式,则无法强制其进入纵向模式,反之亦然。您所能做的只是告诉应用程序处于横向或纵向的特定模式。您可以通过以下方式实现:-

  • 您应该实现
    shouldAutorotateToInterfaceOrientation

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    { 
        // Return YES for supported orientations
        return UIInterfaceOrientationIsLandscape(interfaceOrientation);
    }
    
  • 编辑pList

    在plist中添加一个键
    UIInterfaceOrientation
    ,并根据需要将其属性更改为
    UIInterfaceOrientation和scapeLeft
    UIInterfaceOrientation和scapeRight
    UIInterfaceOrientation肖像向上向下


  • 如果Inder Kumar Rathore的建议有效,那就太好了。但文档中描述的方向属性是只读的,因此如果它能工作,我不确定你将来是否能依靠它工作(除非苹果做了聪明的事情并改变了这一点;强迫方向改变,不管用户当前如何拿着他们的设备,这是一个明显的功能需求)

    作为替代方案,插入viewDidLoad中的以下代码将成功(并且有点奇怪)强制定向(假设您已经按照roronoa zorro的建议修改了AutoRotateTointerFaceOrientation):

    显然,如果用户当前以纵向方式握住设备,则会发生这种情况(因此您的shouldAutorotateToInterfaceOrientation可能仅为横向设置,如果用户以纵向方式握住设备,则此例程将其切换为横向)。如果您的ShouldAutorotationPointerFaceOIRentation设置为仅用于纵向,则只需将UIDeviceOrientationSportRait与UIDeviceOrientationIsLandscape交换即可


    出于某种原因,从主窗口中删除视图,然后重新添加视图会强制它查询shouldAutorotateToInterfaceOrientation并正确设置方向。考虑到这不是苹果认可的方法,也许人们应该避免使用它,但它对我来说是有效的。您的里程可能会有所不同。但这也涉及到其他技术。

    如果英德·库马尔·拉索尔的建议有效,那就太好了。但文档中描述的方向属性是只读的,因此如果它能工作,我不确定你将来是否能依靠它工作(除非苹果做了聪明的事情并改变了这一点;强迫方向改变,不管用户当前如何拿着他们的设备,这是一个明显的功能需求)

    作为替代方案,插入viewDidLoad中的以下代码将成功(并且有点奇怪)强制定向(假设您已经按照roronoa zorro的建议修改了AutoRotateTointerFaceOrientation):

    显然,如果用户当前以纵向方式握住设备,则会发生这种情况(因此您的shouldAutorotateToInterfaceOrientation可能仅为横向设置,如果用户以纵向方式握住设备,则此例程将其切换为横向)。如果您的ShouldAutorotationPointerFaceOIRentation设置为仅用于纵向,则只需将UIDeviceOrientationSportRait与UIDeviceOrientationIsLandscape交换即可

    出于某种原因,从主窗口中删除视图,然后重新添加视图会强制它查询shouldAutorotateToInterfaceOrientation并正确设置方向。考虑到这不是苹果认可的方法,也许人们应该避免使用它,但它对我来说是有效的。您的里程可能会有所不同。但这也涉及到其他技术。

    在iOS7上工作得非常完美

    如果您想要Potrait,那么:

    [[UIDevice currentDevice] setValue:
                          [NSNumber numberWithInteger: UIInterfaceOrientationPortrait]
                                forKey:@"orientation"];
    
    如果您想要景观:

    [[UIDevice currentDevice] setValue:
                          [NSNumber numberWithInteger: UIInterfaceOrientationLandscapeRight]
                                forKey:@"orientation"];
    
    在iOS7上工作得非常完美

    如果您想要Potrait,那么:

    [[UIDevice currentDevice] setValue:
                          [NSNumber numberWithInteger: UIInterfaceOrientationPortrait]
                                forKey:@"orientation"];
    
    如果您想要景观:

    [[UIDevice currentDevice] setValue:
                          [NSNumber numberWithInteger: UIInterfaceOrientationLandscapeRight]
                                forKey:@"orientation"];
    

    它工作是因为您必须使用
    attemptRotationToDeviceOrientation
    来设置设备方向。您的意思是它不工作?此外,对于attemptRotationToDeviceOrientation,我无法强制它指向特定方向(即:提供我想要的方向),它正在工作,因为您必须使用
    attemptRotationToDeviceOrientation
    指向设备方向。您的意思是它不工作?此外,由于尝试旋转方向,我无法将其强制为特定方向(即:提供我想要的方向)@bharathgangupalli是的,它可以工作,但不知道为什么我仍然得到2张反对票:(@inderkumarratore
    setOrientation
    在中不推荐使用。)iOS6@jeff9888有什么新的方法可以做到这一点吗?如果你找到了,请分享。@bharathgangupalli是的,它很有效,但不知道为什么我仍然得到了2张反对票:(@inderkumarratore
    setOrientation
    Is dep