【问题标题】:How to change the orientation of the app without changing the device orientation in iphone app如何在不更改 iPhone 应用程序中的设备方向的情况下更改应用程序的方向
【发布时间】:2013-04-26 13:36:04
【问题描述】:

我想更改应用程序的方向而不更改 iphone 应用程序中的设备方向。 我想以编程方式将视图从纵向模式更改为横向模式。

还想知道苹果商店会接受吗?

谢谢

现在我从其他人那里得到了如下解决方案

[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];

当您添加这一行时,会出现一个警告,要删除此警告,只需在您的实现文件中添加以下代码。..

@interface UIDevice (MyPrivateNameThatAppleWouldNeverUseGoesHere)


    - (void) setOrientation:(UIInterfaceOrientation)orientation;


    @end

然后在下面的方法中,如果需要,只需编写此代码..

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

    {

        return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);

    }

但现在想知道这是否被苹果应用商店接受?

谢谢

【问题讨论】:

    标签: iphone objective-c ios


    【解决方案1】:

    使用此行以编程方式更改方向...

    [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];
    

    当你添加这一行时,会出现一个警告,要删除这个警告,只需在你的实现文件中添加下面的代码..

    @interface UIDevice (MyPrivateNameThatAppleWouldNeverUseGoesHere)
    - (void) setOrientation:(UIInterfaceOrientation)orientation;
    @end
    

    然后在下面的方法中,如果需要,只需编写此代码..

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
        // Return YES for supported orientations
        //    return NO;
        return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
    }
    

    希望对你有帮助..

    :)

    【讨论】:

    • 您的代码没有任何变化,UI 没有任何变化。UI 应该从纵向变为横向
    • 只要在你 viewWillAppear: method .. [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];
    • 我想在点击按钮时改变方向。我有 shouldAutorotatetoInterFaceorientation 返回是。
    • 我刚刚开始工作..... :-) 现在只想知道苹果应用商店是否可以接受它?谢谢你
    • 最受欢迎的伙伴.. 但现在我不确定这是否被苹果接受,但对于 setOrientation 方法,如果上述代码未实现,它只会发出警告..
    【解决方案2】:

    添加类变量

    Bool isInLandsCapeOrientation;
    

    在 viewDidLoad 中

    将此标志设置为

    isInLandsCapeOrientation = false;
    

    添加如下函数

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
         if (!isInLandsCapeOrientation) {
             return (UIInterfaceOrientationIsPortrait(interfaceOrientation));
    
         }else {
             return  (UIInterfaceOrientationIsLandscape(interfaceOrientation));
         }
    }
    

    要将方向从纵向更改为横向,让它发生在按钮操作上

     - (IBAction)changeOrientationButtonPressed:(UIButton *)sender
    {
        isInLandsCapeOrientation = true;
        UIViewController *viewController = [[UIViewController alloc] init];
        [self presentModalViewController:viewController animated:NO];
        [self dismissModalViewControllerAnimated:NO];
    }
    

    这对我来说很好。

    【讨论】:

      【解决方案3】:

      要将方向纵向模式更改为横向模式,请使用此代码

      - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
      {
          // Return YES for supported orientations
          return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight);
      }
      

      使用此代码以编程方式更改方向...

      [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];
      

      【讨论】:

        【解决方案4】:

        如果您只想在横向更改特定视图..那么您可以在其 viewDidLoad 中尝试以下操作

            float   angle = M_PI / 2; 
            CGAffineTransform transform = CGAffineTransformMakeRotation(angle);
           [ [self.view] setTransform:transform];
        

        【讨论】:

          【解决方案5】:

          文档将方向属性描述为只读,因此如果它有效,我不确定您是否可以依赖它在未来工作(除非 Apple 做了聪明的事情并改变了这一点;强制改变方向,不管用户当前如何握住他们的设备,这是一个明显的功能需求)。

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

          if (UIDeviceOrientationIsPortrait([[UIDevice currentDevice] orientation]))
          {
              UIWindow *window = [[UIApplication sharedApplication] keyWindow];
              UIView *view = [window.subviews objectAtIndex:0];
              [view removeFromSuperview];
              [window addSubview:view];
          }
          

          很明显,如果用户当前以纵向模式持有他们的设备(因此可能您的shouldAutorotateToInterfaceOrientation 仅设置为横向,并且如果用户以纵向模式持有他们的设备,此例程会将其转换为横向) )。如果您的shouldAutorotateToInterfaceOirentation 仅设置为纵向,您只需将UIDeviceOrientationIsPortraitUIDeviceOrientationIsLandscape 交换即可。

          出于某种原因,从主窗口中删除视图然后重新添加它会强制它查询shouldAutorotateToInterfaceOrientation 并正确设置方向。鉴于这不是 Apple 认可的方法,也许人们应该避免使用它,但它对我有用。你的旅费可能会改变。但这也涉及其他技术。查看 SO discussion

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2014-06-24
            • 2020-01-05
            • 2017-09-15
            • 2018-11-21
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-12-12
            相关资源
            最近更新 更多