【问题标题】:How to set iOS device orientation only landscape?如何设置iOS设备方向仅横向?
【发布时间】:2013-05-14 15:02:50
【问题描述】:

我正在使用XcodeMacOS 下编写一个简单的应用程序。我希望我的应用程序始终处于横向模式。所以我在项目属性中设置了横向。但是当我向我的window.subview 添加一个按钮时,这个按钮不会出现在横向位置。

我只有 AppDelegate 类。我改变了功能: - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

我已添加:

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor purpleColor];
[self.window makeKeyAndVisible];

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setBackgroundColor:[UIColor whiteColor]];
[button setFrame:CGRectMake([[UIScreen mainScreen] bounds].size.width/2 - 50, [[UIScreen mainScreen] bounds].size.height/2,
                            100, 100)];
[button setTitle:@"Button" forState:UIControlStateNormal];
[self.window addSubview:button];

更新:我添加了 [self.window setTransform:CGAffineTransformMakeRotation(-M_PI/2)];

并得到:

【问题讨论】:

  • 请也发布您的代码
  • 在界面生成器中检查界面方向
  • 您是如何在视图控制器中为iOS6+ 定义–shouldAutorotate 方法,以及如何为<iOS6 定义–shouldAutorotateToInterfaceOrientation:

标签: ios ipad uikit uiinterfaceorientation


【解决方案1】:

选择您的目标项目并确保您选择了以下横向模式:

【讨论】:

    【解决方案2】:

    我已经这样解决了:

    1. 创建我自己的 RootViewController(如How to create root view controller

    2. 然后:

      self.rootController = [[AppRootViewController alloc] init];
      
      self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
      self.window.rootViewController = self.rootController;
      [self.window addSubview:self.rootController.view];
      
      UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
      ....
      
      [[[self.window rootViewController] view] addSubview:button];
      

    【讨论】:

      【解决方案3】:

      有两种机制可以控制这一点。

      您已经了解目标属性。如果这就是你改变的全部,这应该照顾它。

      还有一个较低级别的控件,您可能是从旧模板代码中获取的。对于每个视图控制器,如果你实现了方法

      shouldRotateToInterfaceOrientation:
      

      这将覆盖目标的设置。我怀疑超类的实现只是指目标设置。

      【讨论】:

        【解决方案4】:

        您可以在YourProject-info.plist中添加UISupportedInterfaceOrientations属性如下,那么它将只支持横向。

        <key>UISupportedInterfaceOrientations</key>
        <array>
            <string>UIInterfaceOrientationLandscapeRight</string>
            <string>UIInterfaceOrientationLandscapeLeft</string>
        </array>
        

        但是不知道你的两张图是什么意思

        【讨论】:

        • 我已经这样做了,但只有添加我自己的根控制器才有帮助。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-02-11
        • 2015-11-13
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多