我创建了一个 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 方法并以编程方式完成了这个项目。请回复我。