2011-10-17 24 views
1

我有一個iPad應用程序與分割視圖控制器。爲什麼detailViewController框架總是返回我1024 * 768

我想programmaticaly創建並添加子視圖到detailViewController在右下角。這樣做,我試圖讓的DetailView的框架(應用程序支持自動旋轉,所以那個位置不是靜態的)

我接下來做

在viewWillAppear中

爲的DetailView我試圖讓幀,並計算位置,我需要

- (void)viewWillAppear:(BOOL)animated { 

    [super viewWillAppear:animated];  

    CGRect btnRect = self.view.frame; 
    //it always return 1024*748 width and height 
    //even in landscape mode 
    //when as i think must return 1024-321=703*748 pxls 
    //where is my mistake? How i can get actual frame 
    //dimensions for detailViewController in landscape orientation 

    btnRect.origin.y = btnRect.size.height - 42; 
    btnRect.origin.x = btnRect.size.width - 42; 
    btnRect.size.height = 42; 
    btnRect.size.width = 42; 

    UIButton* btn = [UIButton buttonWithType:UIButtonTypeCustom]; 
[btn setBackgroundImage:someimage forState:UIControlStateNormal]; 
[[btn layer] setFrame:btnRect]; 

    [self.view addSubview:btn]; 
} 

但它總是告訴我detailView框架有1024 * 748尺寸。在風景模式下,我認爲它必須是703 * 748。我需要做什麼來獲得實際的detailView框架?

+0

有你在代碼中設置或Interface Builder中右自動調整大小面具? – jbat100

+0

不是。最佳做法是什麼? –

+0

view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;或在界面生成器中(在視圖的維度選項卡中) – jbat100

回答

2

您應該想要更改shouldAutorotateToInterfaceOrientation方法中視圖的框架。

一個例子: (這人會調整scrollView2,以適應新的方向(88個像素下則MAINVIEW))

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    // Return YES for supported orientations 

    if (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) { 

     // Portrait 
     self.scrollView2.frame = CGRectMake(0, 88, [UIScreen mainScreen].bounds.size.width, self.view.frame.size.height - 88); 
     [self drawContent]; 
    } 
    else { 

     // Landscape 
     self.scrollView2.frame = CGRectMake(0, 88, [UIScreen mainScreen].bounds.size.height, self.view.frame.size.width); 
     [self drawContent]; 
    } 


    return YES; 
} 
+0

首先,我必須創建子視圖,並將其放在父視圖上,但我無法獲得正確的尺寸父視圖來定位子視圖,我需要。 –

+0

Thanx。我檢查方向並創建與之相關的按鈕。 –