简体   繁体   English

如何设置iOS设备方向仅景观?

[英]How to set iOS device orientation only landscape?

I'm writing a simple app under MacOS using Xcode . 我正在使用XcodeMacOS下编写一个简单的应用程序。 I want my app to be in landscape mode always. 我希望我的应用程序始终处于横向模式。 So I set landscape orientation in project properties. 所以我在项目属性中设置横向方向。 But when I add a button to my window.subview , this button doesn't appear in landscape position. 但是当我向window.subview添加一个按钮时,此按钮不会出现在横向位置。

I have only AppDelegate class. 我只有AppDelegate类。 I've changed function: - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 我改变了函数: - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

I've added: 我已经添加:

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

在此输入图像描述

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

and got: 得到了: 在此输入图像描述

Select your target project and make sure you have choose following landscape mode: 选择目标项目并确保选择以下横向模式:

在此输入图像描述

I've solved it like this: 我已经解决了这个问题:

  1. create my own RootViewController (like in How to create root view controller ) 创建我自己的RootViewController(如在如何创建根视图控制器中

  2. then: 然后:

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

There are two mechanisms that control this. 有两种机制可以控制它。

You already know about the target properties. 您已经了解了目标属性。 If this is all you change, this should take care of it. 如果你改变了这一切,那就应该好好处理。

There is also a lower level control, which you might have picked up from old template code. 还有一个较低级别的控件,您可能从旧的模板代码中获取了该控件。 For each view controller, if you implement the method 对于每个视图控制器,如果实现该方法

shouldRotateToInterfaceOrientation:

That will override the setting for the target. 这将覆盖目标的设置。 I suspect the superclasses implementation just refers to the target setting. 我怀疑超类实现只是引用了目标设置。

You can add UISupportedInterfaceOrientations property in YourProject-info.plist as follow, then it will only support landscape orientation. 您可以在YourProject-info.plist中添加UISupportedInterfaceOrientations属性,如下所示,然后它将仅支持横向方向。

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

but do not know what's the meaning with your two pictures 但不知道你的两张照片是什么意思

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果应用程序设备的方向是纵向的,那么如何仅在iOS中为一个视图构建者设置强制横向,Swift 2.3 - if app device orientation is portrait how to set force landscape only for one view constroller in ios, swift 2.3 将设备方向设置为仅适用于iPad的横向 - Set device orientation as landscape for iPad only iOS-iPad Air和iPad Pro仅在横向模式下强制设备定向 - iOS - iPad Air and iPad Pro force device orientation in landscape only iOs将故事板方向设置为横向 - iOs set Storyboard orientation to landscape 如何以编程方式仅对6和6Plus设备启用横向定位? - How to enable landscape orientation only for 6 and 6Plus device programmatically? IOS设备方向卡在纵向模式下,没有横向 - IOS Device Orientation Stuck at Portrait mode, no Landscape Objective c 如何在ios13中将方向设置为横向 - Objective c How to set orientation to landscape in ios13 如何使用UINavigationController(Swift 2.0 iOS 9)强制设置风景方向 - How to force set Landscape Orientation with UINavigationController (Swift 2.0 iOS 9) 在 ios 6 上仅在一个视图中启用横向 - enable landscape orientation in only one view on ios 6 iOS 7 XCode 5故事板将方向设置为横向 - iOS 7 XCode 5 Storyboard set orientation to Landscape
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM