2010-01-31 57 views
0

我已經建立了一個應用程序,並使用一些基本的代碼從「iPhone開發的食譜」,以創建一個簡單的觀點改變的代碼。該應用程序在縱向模式下正常工作,但旋轉時,框的尺寸保持不變。這裏是我的代碼:設置視圖與方向

- (void)loadView { 
    //Create the full application frame (minus the status bar) - 768, 1004 
    CGRect fullRect = [[UIScreen mainScreen] applicationFrame]; 

    //Create the frame of the view ie fullRect-(tabBar(49)+navBar(44)) - 768, 911 
    CGRect viewRect = CGRectMake(0, 64, fullRect.size.width, fullRect.size.height-93.0); 

    //create the content view to the size 
    contentView = [[UIView alloc] initWithFrame:viewRect]; 
    contentView.backgroundColor = [UIColor whiteColor]; 

    // Provide support for autorotation and resizing 
    contentView.autoresizesSubviews = YES; 
    contentView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight); 

    self.view = contentView; 
    [contentView release]; 

    // reset the origin point for subviews. The new origin is 0,0 
    viewRect.origin = CGPointMake(0.0f, 0.0f); 

    // Add the subviews, each stepped by 32 pixels on each side 
    UIView *subview = [[UIView alloc] initWithFrame:CGRectInset(viewRect, 32.0f, 32.0f)]; 
    subview.backgroundColor = [UIColor lightGrayColor]; 
    [contentView addSubview:subview]; 
    [subview release]; 

    subview = [[UIView alloc] initWithFrame:CGRectInset(viewRect, 64.0f, 64.0f)]; 
    subview.backgroundColor = [UIColor darkGrayColor]; 
    [contentView addSubview:subview]; 
    [subview release]; 

    subview = [[UIView alloc] initWithFrame:CGRectInset(viewRect, 96.0f, 96.0f)]; 
    subview.backgroundColor = [UIColor blackColor]; 
    [contentView addSubview:subview]; 
    [subview release]; 
} 

有沒有什麼辦法讓這個重新加載新的尺寸時,它是旋轉或更好的方式適應方向改變?

感謝 JP

回答

1

我可能會誤解你的問題。用「盒子」,你的意思是子視圖嗎?

如果是這樣,子視圖旋轉時將保留它們的外觀尺寸,因爲它們是正方形。

在任何情況下,當一個視圖被第一初始化,通常是從筆尖viewDidLoad才調用。如果您需要在視圖旋轉時更改視圖,則需要在willRotateToInterfaceOrientation:duration:和/或didRotateFromInterfaceOrientation:中進行更改。

要更改任何視圖的尺寸,簡單地重置其幀。

+0

非常感謝TechZen。這是我正在尋找的兩種方法。無法在文檔中找到它們。 – Jack

1

object.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;