簡體   English   中英

如何設置iOS設備方向僅景觀?

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

我正在使用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)];

得到了: 在此輸入圖像描述

選擇目標項目並確保選擇以下橫向模式:

在此輸入圖像描述

我已經解決了這個問題:

  1. 創建我自己的RootViewController(如在如何創建根視圖控制器中

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

有兩種機制可以控制它。

您已經了解了目標屬性。 如果你改變了這一切,那就應該好好處理。

還有一個較低級別的控件,您可能從舊的模板代碼中獲取了該控件。 對於每個視圖控制器,如果實現該方法

shouldRotateToInterfaceOrientation:

這將覆蓋目標的設置。 我懷疑超類實現只是引用了目標設置。

您可以在YourProject-info.plist中添加UISupportedInterfaceOrientations屬性,如下所示,然后它將僅支持橫向方向。

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

但不知道你的兩張照片是什么意思

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM