2009-02-08 65 views
4

我正嘗試使用實用程序模板創建始終處於橫向模式的iPhone應用程序。下面是我所做的:iPhone橫向專用實用程序 - 模板應用程序

  • 創建一個新的iPhone應用程序項目,使用實用工具應用程序模板
  • 在Interface Builder中,旋轉各方面的意見90度。
  • 在Interface Builder中,向MainView的中間添加一個標籤。在整個視圖中一直拉伸,將對齊設置爲居中,並設置自動彈簧,使其可以水平拉伸。
  • 在Info.plist中,添加鍵 「UIInterfaceOrientation」 與值 「UIInterfaceOrientationLandscapeRight」
  • 在控制器類,改變shouldAutorotateToInterfaceOrientation方法 「返回(interfaceOrientation == UIInterfaceOrientationLandscapeRight)||(interfaceOrientation == UIInterfaceOrientationLandscapeLeft);」
  • 運行該應用程序。

當我啓動我的應用程序時,它出現在橫向方向,但主視圖只覆蓋顯示的上半部分,並且它被水平拉伸。我在模擬器和實際設備上都得到了相同的結果。我已經看到了SDK的2.2和2.2.1版本。

我已經能夠通過將以下步驟上述以解決該問題:

  • 添加「self.view.autoresizesSubviews = NO;」到「[super viewDidLoad];」之後的RootViewController的viewDidLoad方法。

如果我這樣做,那麼它按預期工作。但是這感覺像一個黑客。爲什麼這是必要的?

我不認爲這是一個轉型問題。所有元素都以正確的方向繪製,並具有適當的縮放比例。問題似乎是主視圖的邊界矩形變得時髦。看起來主視圖的高度被削減了一半多,寬度增加了大約50%。

如果我使用基於視圖的應用程序模板而不是實用程序來執行完全相同的一組步驟,則一切都按預期工作。所以我很確定這個問題是由一個Utility應用程序管理其視圖的具體情況決定的。

有人明白這裏發生了什麼?

+0

一種愚蠢的問題,但是在IB查看正確的大小? – 2009-02-08 14:10:39

+0

是的,大小是正確的。 – 2009-02-08 16:44:22

回答

3

我想說設置這個鍵不會旋轉你的界面;您仍然需要在橫向模式下佈局內容並使用CFAffineTransform進行適當的旋轉 - 請參閱iPhone OS編程指南中的「在橫向模式下啓動」。爲了找到你的參考資料,我發現了這樣一條評論:「要在v2.1之前的iPhone OS版本中以橫向模式啓動基於視圖控制器的應用程序,需要將90度旋轉應用於應用程序的轉換在iPhone OS 2.1之前,視圖控制器不會根據UIInterfaceOrientation鍵的值自動旋轉它們的視圖,但這一步在iPhone OS 2.1和更高版本中不是必需的。

因此,如果您運行的是2.1之前版本,則需要將此代碼添加到您的視圖控制器中的viewDidLoad方法中。 (否則,你可以發佈一些代碼?)

-(void)viewDidLoad 
// After loading the view, transform the view so that the co-ordinates are right for landscape 
// As described in iPhone Application Programming Guide 
// Weird, I'm sure this used to be needed, but it doesn't now. The one in CardScrollViewController is needed though. 
{ 
    [super viewDidLoad]; 

    CGAffineTransform transform = self.view.transform; 
    CGPoint center = CGPointMake(kScreenHeight/2.0, kScreenWidth/2.0); 

    // Set the center point of the view to the center point of the window's content area. 
    self.view.center = center; 

    // Rotate the view 90 degrees around its new center point. 
    transform = CGAffineTransformRotate(transform, (M_PI/2.0)); 
    self.view.transform = transform;  
} 
2

簡描述UIInterfaceOrientation至UIInterfaceOrientationLandscapeRight(或UIInterfaceOrientationLandscapeLeft),以及在文檔中所建議的旋轉設置的設定,但我在我的根視圖控制器使用的代碼稍微不同的塊(以相同的端部) :

- (void)loadView 
{ 
    UIView *primaryView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]]; 
    primaryView.backgroundColor = [UIColor clearColor]; 

    // Start in landscape orientation, and stay that way 
    UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation]; 
    if (orientation == UIInterfaceOrientationLandscapeRight) 
    { 
     CGAffineTransform transform = primaryView.transform; 

     // Use the status bar frame to determine the center point of the window's content area. 
     CGRect statusBarFrame = [[UIApplication sharedApplication] statusBarFrame]; 
     CGRect bounds = CGRectMake(0, 0, statusBarFrame.size.height, statusBarFrame.origin.x); 
     CGPoint center = CGPointMake(60.0, bounds.size.height/2.0); 

     // Set the center point of the view to the center point of the window's content area. 
     primaryView.center = center; 

     // Rotate the view 90 degrees around its new center point. 
     transform = CGAffineTransformRotate(transform, (M_PI/2.0)); 
     primaryView.transform = transform; 
    } 

    self.view = primaryView; 
    [primaryView release]; 
} 

除此之外,我在根視圖控制器中實現的以下委託方法:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    return ((interfaceOrientation == UIInterfaceOrientationLandscapeLeft) || (interfaceOrientation == UIInterfaceOrientationLandscapeRight)); 
} 

最後,我遇到與模擬器不會自動旋轉正確怪異的毛刺,所以我需要實現我的UIApplicationDelegate以下的委託方法:

- (void)application:(UIApplication *)application willChangeStatusBarOrientation:(UIInterfaceOrientation)newStatusBarOrientation duration:(NSTimeInterval)duration; 
{ 
    // This prevents the view from autorotating to portrait in the simulator 
    if ((newStatusBarOrientation == UIInterfaceOrientationPortrait) || (newStatusBarOrientation == UIInterfaceOrientationPortraitUpsideDown)) 
     [application setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO]; 
} 

畢竟,我的應用程序能夠在橫向啓動(右鍵)並保持在2.0固件和模擬器下的方向。

0

嘗試將視圖的方向屬性設置爲筆尖中的橫向。該屬性可以在模擬矩陣下的UIView的信息視圖的第4個選項卡[屬性檢查器]中找到。