Search code examples
iphoneiosuiviewuiviewcontrollercgrectmake

Is there a way to dynamically change views to fill the screen?


In my iOS app, I have three web views defined inside another view. Two of the web views may not always be used, so is there a way to ask the remaining views to fill the rest of the screen? The equivalent in android would be when you use "fill_parent."

Right now I'm doing it all by changing the frames which is really cumbersome.

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

Is there a way to make the mainView use the rest of the screen without explicitly setting the frame?


Solution

  • Instead of hard-coding in the values, you could also put some code in that looks at the sizes of what is being displayed and calculates what size your views need to be from that. It might look something like:

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

    Repeat for other 2 views, then

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

    This way is still kinda clunky, but it will work a little better if you want to resize things in the future.

    EDIT: I cleaned up the syntax, it should be correct now.