为什么detailViewController的框架总是返回1024 * 768

时间:2011-10-17 11:16:19

标签: objective-c ipad uiview uisplitviewcontroller

我有一个带分割视图控制器的iPad应用程序。

我想在右下角创建并添加子视图到detailViewController。为此,我尝试获取detailView框架(应用程序支持自动旋转,以便该位置不是静态的)

我做下一个

在viewWillAppear for 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框架?

1 个答案:

答案 0 :(得分:2)

您应该在shouldAutorotateToInterfaceOrientation方法中更改视图的框架。

一个例子: (这个将调整scrollView2以适应其新方向(比主视图低88个像素))

- (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;
}