【问题标题】:Is there a way to dynamically change views to fill the screen?有没有办法动态更改视图以填充屏幕?
【发布时间】:2012-09-13 19:04:23
【问题描述】:

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

现在我都是通过改变框架来完成的,这真的很麻烦。

// 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 在不显式设置框架的情况下使用屏幕的其余部分?

【问题讨论】:

    标签: iphone ios uiview uiviewcontroller cgrectmake


    【解决方案1】:

    除了在值中进行硬编码之外,您还可以在其中添加一些代码来查看正在显示的内容的大小并从中计算您的视图需要的大小。它可能看起来像:

    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);
    

    这种方式仍然有点笨拙,但如果你以后想调整大小,它会更好一些。

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

    【讨论】:

    • 我最终做了类似的事情,我只是不想这样做。谢谢。
    【解决方案2】:

    如果你想让你的应用需要 iOS 6,你可以使用自动布局来做到这一点。

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

    【讨论】:

      猜你喜欢
      • 2016-01-01
      • 2012-02-20
      • 2020-08-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-05
      • 1970-01-01
      相关资源
      最近更新 更多