2012-05-18 55 views
1

我正在編寫一個iPad應用程序,需要知道用於繪圖目的的視圖的可用區域。視圖被添加到導航控制器中,所以我的狀態欄加上導航控制器都佔用了一定數量的像素。我的應用恰好處於橫向模式,儘管我不認爲這是相關的。狀態欄和導航欄後的可用視圖大小

我能夠在旋轉後使用didRotateFromInterfaceOrientation獲得正確的視圖大小。但我不知道如何在沒有旋轉屏幕的情況下做到這一點。

- (void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
    [self.view setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; 
    NSLog(@"drfi %d %d", (int)self.view.frame.size.width, (int)self.view.frame.size.height); 

} 

^^旋轉後起作用。之前沒有。無法弄清楚如何獲得準確的數字。我真的不想硬接線。

我還需要這個功能,以獨立於設備 - 它應該在新的iPad以及較舊的iPad分辨率上工作。一旦我知道確切的可用區域,我就可以處理擴展問題。爲什麼這麼難?幫幫我!!

+0

您是否有理由在旋轉之前進行此操作?旋轉後,你不能簡單地重畫有問題的視圖嗎? – Tim

+0

是的 - 非常簡單,可能永遠不會有旋轉! – Goblortikus

+0

嘗試在viewDidAppear中執行計算。 –

回答

1

我不認爲你需要指定didRotateFromInterfaceOrientation什麼,我會建議,而不是一些屬性被設置爲您的視圖自動調整面罩,使其根據您的視圖方向自動調整自身內部的框架的觀點。

通過時,您的看法是加載例如將此項設置爲您的觀點(viewDidLoad方法):

self.view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth; 

可以指定自己的觀點會自動改變其寬度和高度,並能得到正確的價值觀,你需要從那裏出發。

你應該閱讀:爲更好地理解意見的iOS

編輯http://developer.apple.com/library/ios/#documentation/WindowsViews/Conceptual/ViewPG_iPhoneOS/CreatingViews/CreatingViews.html#//apple_ref/doc/uid/TP40009503-CH5-SW1

而且你可能想找出什麼是你的設備可與[[UIApplication sharedApplication] statusBarOrientation];

來完成定位
+0

唉......它不起作用。 autoresizingMask設置將正確考慮狀態欄(20像素),但不考慮導航欄。 – Goblortikus

+0

您可以訪問statusBar維度並估計您從那裏查看的區域的可用大小嗎?只是一個建議 – tiguero

1

你的應用程序看起來像:有一個啓動視圖,然後在這個視圖中你將加載並添加一個主視圖到窗口中,對吧?那麼你應該如下在你的主視圖:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     CGRect frame = self.view.frame; 
     frame.origin.y = frame.origin.y + 20.0; 
     self.view.frame = frame; 
    } 
    return self; 
} 
+0

這沒有得到正確的區域...應用程序首先創建一個導航控制器,然後在其中的表視圖控制器,然後當用戶選擇一個選項時,它將UIView作爲圖形繪圖表面推送到navigationViewController。這是最後的UIView,我無法獲得合適的尺寸。 – Goblortikus

+0

你是如何將「UIView」「推」到NavigationController中的?你的意思是「UIViewController」嗎?您應該將該圖形視圖放置到ViewController中,然後使用;將其推入到NavigationController中; [controller.navigationController pushViewController:viewController animated:YES]; –

1

試試這個。

CGRect frame = [UIScreen mainScreen].bounds; 
CGRect navFrame = [[self.navigationController navigationBar] frame]; 
/* navFrame.origin.y is the status bar's height and navFrame.size.height is navigation bar's height. 
So you can get usable view frame like this */ 
frame.size.height -= navFrame.origin.y + navFrame.size.height; 
1

您可以通過一個實例方法與類別相結合的方法得到這個動態:

實例方法:

這假定您的視圖控制器(個體經營)嵌入導航控制器內。

-(int)getUsableFrameHeight { 
    // get the current frame height not including the navigationBar or statusBar 
    return [MenuViewController screenHeight] - [self.navigationController navigationBar].frame.size.height; 
} 

Class類方法:

+(CGFloat)screenHeight { 
    CGFloat screenHeight; 
    // it is important to do this after presentModalViewController:animated: 
    if ([[UIApplication sharedApplication] statusBarOrientation] == UIDeviceOrientationPortrait || 
     [[UIApplication sharedApplication] statusBarOrientation] == UIDeviceOrientationPortraitUpsideDown){ 
     screenHeight = [UIScreen mainScreen].applicationFrame.size.height; 
    } else { 
     screenHeight = [UIScreen mainScreen].applicationFrame.size.width; 
    } 
    return screenHeight; 
} 

以上將始終如一地爲您提供可用的框架高度的狀態欄和導航欄已被刪除之後,在縱向和橫向。

注:類方法將自動扣除20 PT狀態欄 - 然後我們只減去導航標題可變高度(32磅爲景觀,44磅爲縱向)。