有没有办法动态更改视图以填充屏幕?

时间:2012-09-13 19:04:23

标签: iphone ios uiview uiviewcontroller cgrectmake

在我的iOS应用中,我在另一个视图中定义了三个Web视图。可能并不总是使用其中两个Web视图,因此有没有办法让剩余的视图填满屏幕的其余部分? android中的等价物就是当你使用“fill_parent”时。

现在我正在通过改变那些非常繁琐的帧来做到这一切。

// There is no top banner, but it does use the footer
topBannerView.frame = CGRectMake(0, 0, 320, 0);
mainView.frame = CGRectMake(0, 0, 320, 400);
footerView.frame = CGRectMake(0, 400, 320, 60);

有没有办法让mainView使用屏幕的其余部分而不明确设置框架?

2 个答案:

答案 0 :(得分:0)

如果您想让自己的应用需要iOS 6,可以使用autolayout执行此操作。

如果你想在iOS 5上运行,你必须在代码中运行。

答案 1 :(得分:0)

除了对值进行硬编码外,您还可以放入一些代码来查看正在显示的内容的大小,并计算您的视图所需的大小。它可能看起来像:

if(!topBannerView.hidden)
{
  topBannerHeight = topBannerView.bounds.size.height; //syntax here might be a little off, can't test code right now, but I think it's close to this
}
else
{
  topBannerHeight = 0;
}

对其他2个视图重复,然后

topBannerView.frame = CGRectMake(0, 0, 320, topBannerHeight);
mainView.frame = CGRectMake(0, topBannerHeight, 320, mainViewHeight);
footerView.frame = CGRectMake(0, (mainViewHeight+topBannerHeight), 320, footerViewHeight);

这种方式仍然有点笨拙,但如果你想在未来重新调整大小,它会更好一些。

编辑:我清理了语法,现在应该是正确的。

相关问题