0

我创建了一个 UIViewController 和一个 UIView,其中有两个 UIButtons。

self.view = [[UIView alloc]initWithFrame:CGRectMake( 0,0,320,480)];
self.view.autoresizesSubviews = YES;

self.view.backgroundColor = [UIColor redColor];
UIView *view;
view = [[UIVew alloc]initWithFrame:CGRectMake(0,0,320,480)];
view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
UIButton *button1;
button.frame = CGRectMake(100,130,120,50);
[button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[view addSubview:button];
[controller addSubview:view];

// […]

- (void)buttonAction:(id)sender
{
    // I have created another view for a purpose
    [view removeFromSuperview];
    UIView *view_obj;
    view_obj.backgroundColor = [UIColor greenColor];
    view_obj.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleWidth;
    [self.view addSubview:view_obj];  
}

问题:当我将方向更改为横向时,具有按钮的视图会调整大小,但是当我单击按钮时,会显示带有 blueColor 的视图而不会自动调整大小。我可以在 redColor 中看到一半作为根视图,在 greenColor 中看到一半作为子视图。我还覆盖了 ShouldAutoRotate 方法并以编程方式完成了这个项目。请回复我。

4

2 回答 2

0

在 Interface Builder 中,选择视图对象,然后在检查器窗口的“视图属性”窗格中确保您选择了状态栏和“导航栏”的“顶部栏”(假设您的应用程序有导航栏)。您将在“模拟用户界面元素”部分的顶部附近看到这些。这会通知应用程序在确定 UIViewController 视图的大小时始终为这些留出空间。

于 2010-08-13T14:57:58.397 回答
-1

试着研究这个问题。

或者

请参阅此问题的以下代码。

- (void)willAnimateSecondHalfOfRotationFromInterfaceOrientation: (UIInterfaceOrientation)fromInterfaceOrientation duration:(NSTimeInterval)duration {    
    UIInterfaceOrientation interfaceOrientation = self.interfaceOrientation;
    if (interfaceOrientation == UIInterfaceOrientationPortrait 
        || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
    {
        button1.frame = CGRectMake(100, 33, 226, 275);
        button2.frame = CGRectMake(440, 33, 226, 275);
        button3.frame = CGRectMake(100, 360, 226, 275);
        button4.frame = CGRectMake(440, 360, 226, 275);
        button5.frame = CGRectMake(100, 693, 226, 275);
        button6.frame = CGRectMake(440, 693, 226, 275);
    }
    else 
    {
        button1.frame = CGRectMake(100, 50, 226, 275);
        button2.frame = CGRectMake(400, 50, 226, 275);
        button3.frame = CGRectMake(700, 50, 226, 275);
        button4.frame = CGRectMake(100, 400, 226, 275);
        button5.frame = CGRectMake(400, 400, 226, 275);
        button6.frame = CGRectMake(700, 400, 226, 275);
    }
}
- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation {
    return (interfaceOrientation || UIInterfaceOrientationPortraitUpsideDown);
}
于 2010-05-21T06:21:46.267 回答