编辑2:当我在没有状态栏的情况下启动应用程序时,一切都按计划行事。有了状态栏,我无法按照我的意愿行事。看起来好像UINavigationController通过减去状态栏的20个像素来保持调整内容视图的大小。我不知道。
我创建了一个简单的基于UINavigationController的应用程序。此导航控制器中的根视图是UITableView。在某个时候,我想从底部以80像素的高视角滑动。顶部的整个视图(由UINavigationController控制的视图)应调整大小并减小80像素,以便为新的底部视图腾出空间。
这基本上是我用来重新定位视图的代码:
-(void)showTeaser {
float adHeight = 80;
[adView setFrame:CGRectMake(0.0,self.navigationController.view.bounds.size.height, 320.0, 80.0)];
[[[UIApplication sharedApplication] keyWindow] addSubview:adView];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
[adView setAlpha:1.0];
[adView setFrame:CGRectMake(0.0,self.navigationController.view.bounds.size.height-adHeight, 320.0, 80.0)];
[self.navigationController.view setFrame:CGRectMake(0.0,0.0, 320.0, self.navigationController.view.bounds.size.height-adHeight)];
[self.view setFrame:CGRectMake(0.0, 0, 320.0, self.view.bounds.size.height-adHeight)];
[UIView commitAnimations]; }
我降低了导航栏的alpha,将UITableviewController的视图设置为红色。新视图是紫色的。
这就是发生的事情。首先截图初始状态。一切看起来都很正常。第二个屏幕截图显示更改帧后的状态。 UITableviewController的视图始终在导航栏下方按20像素。此外,如果我尝试向键窗口添加更多视图,它们总是比我预期的高20个像素。它几乎看起来像键窗口(减去导航栏)向上推20像素。
编辑1:无论我调整视图的大小,它总是20像素。
我是否通过向关键窗口添加视图来犯错误?我不应该这样做吗?
alt text http://www.hans-schneider.de/iphone-seo/1.png alt text http://www.hans-schneider.de/iphone-seo/2.png
答案 0 :(得分:1)
为了解决这个问题,我查看UINavigationController
UIView
的子视图,并手动设置`UINavigationController'视图的边界。
//outerView is a UIView defined in the interface
outerView = [[UIView alloc] initWithFrame:CGRectMake( 0.0, 0.0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);];
//mainNavigationController is a UINavigationController defined in the interface
//rootViewController is a UIViewController (or inherited class) defined in the interface and instanced before this code
mainNavigationController = [[UINavigationController alloc] initWithRootViewController:rootViewController];
//set the frame for the view of the navigation controller - 20 is due to the status bar
mainNavigationController.view.frame = CGRectMake( 0.0, 20.0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 20);
然后,当我去调整大小时,我调整父级'UIView'而不是'UINavigationController'视图。
//change the outer view's frame to resize everything
//adHeight is a float defined earlier
outerView.frame = CGRectMake( 0.0, 20.0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 20 - adHeight);
这在动画序列中效果很好。
编辑2011-05-12:我更新了outerView
框架以填满屏幕。必须将其设置为允许触摸事件。
答案 1 :(得分:0)
您是否尝试过使用tableview的transform属性而不是手动更改它的框架?它可能会更好,因为框架取决于原点,你只想改变它的大小。