2013-05-14 93 views
1

我正在使用XcodeMacOS下編寫一個簡單的應用程序。我希望我的應用始終處於橫向模式。所以我在項目屬性中設置了橫向。但是當我向我的window.subview添加按鈕時,此按鈕不會出現在橫向位置。如何僅設置設備方向橫向?

我只有AppDelegate類。我已經改變了功能: - (BOOL)申請:(UIApplication的*)應用程序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]; 

enter image description here

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

,並得到: enter image description here

+0

也請發表您的代碼接口 – Filoo 2013-05-14 15:04:49

+0

檢查方向在Interface Builder – SAKrisT 2013-05-14 15:08:03

+0

你是怎麼定義'iOS6的+'的'-shouldAutorotate'方法,你是如何在你的視圖控制器中爲'定義'-shouldAutorotateToInterfaceOrientation:'的? – holex 2013-05-16 12:34:23

回答

1

我已經解決了它這樣的:

  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

選擇目標項目,並確保您有以下選擇風景模式:

enter image description here

+0

是的,我做到了,但按鈕仍處於錯誤狀態。 – eilas 2013-05-15 10:48:57

0

有控制這兩種機制。

您已經瞭解目標屬性。如果這是你所有的改變,這應該照顧它。

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

shouldRotateToInterfaceOrientation: 

這將覆蓋目標的設置。我懷疑超類的實現只是指向目標設置。

0

如下所示,您可以在YourProject-info.plist中添加UISupportedInterfaceOrientations屬性,那麼它只會支持橫向。

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

,但不知道是什麼意思你的兩張圖片

+0

我已經這樣做了,但只加入了我自己的根控制器。 – eilas 2013-05-16 10:56:13