[英]How to set iOS device orientation only landscape?
I'm writing a simple app under MacOS
using Xcode
. 我正在使用
Xcode
在MacOS
下编写一个简单的应用程序。 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: 我已经解决了这个问题:
create my own RootViewController (like in How to create root view controller ) 创建我自己的RootViewController(如在如何创建根视图控制器中 )
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.