19

I have a root UIViewController that I am adding other UIViewController's as subviews. Currently each of the subviews are too low down (covering up my custom build tabbar). When I try to so something like the following, it does not work:

// Test setting frame size to see if it works
self.view.frame = CGRectMake(0, 0, 200, 200);

That does nothing to change the frame size.

So, my question is, how can I set my frame when the UIViewController is initialized after it is added as the subview?

Nic Hubbard
  • 41,587
  • 63
  • 251
  • 412

3 Answers3

22

@Nic i think when you are adding that other view, at that time you should define the other views frame size like this:

Someviewcontroller *c = initWithNibName
c.view.frame = CGRectMake(0, 0, 200, 200);
[self addSubView:c];

i dont know if this will work but it is something like this.

manmal
  • 3,900
  • 2
  • 30
  • 42
Robin
  • 10,011
  • 5
  • 49
  • 75
  • how to do it if a uiviewcontroller is a part of a uitabbarcontroller? – wagashi Dec 10 '11 at 16:24
  • I don't know how to set frame of uiviewcontroller when it is a part of a uitabbarcontroller. Is it clear now? – wagashi Dec 11 '11 at 15:55
  • you cannot do that in case of a tabbarcontroller. Ihe frame of the view of a viewcontroller will be set automatically depending on the status bar and navigation bar whether they are shown or not. – Robin Dec 12 '11 at 00:22
  • I see. But I have a uiview that is a subview of uiviewcontroller, should it be possible? I tried setting frame of this uiview 0,0,200,200, but it shows always full size. Why? – wagashi Dec 12 '11 at 09:51
  • If you're having a problem with this, post all your code. It's straightforward and always works, thank goodness. It's very like there is something IN your viewcontroller or view, that is "afterwards" adjusting the size. – Fattie Dec 03 '13 at 09:59
6

First, ensure that your frame is actually not changing size. Likely it /is/ changing size, but you are expecting it to clip its contents; this behavior is not enabled by default on a UIView, and would need to be set via:

  [[self view] setClipsToBounds:YES];

To double check and ensure that your frame is / is not changing size after setting the new frame, try logging this:

  NSLog(@"New frame is: %@", NSStringFromCGRect([[self view] frame]));

or simply setting a breakpoint after the change and inspecting the value in your debugger.

Chris Zelenak
  • 2,158
  • 17
  • 20
  • That seems to work in one of my views, but another view is a UINavigationController, which does not work to change the frame. – Nic Hubbard Feb 08 '11 at 06:18
4

Then there's always...

// Screen, less StatusBar
CGRect l_RectFrame = [UIScreen mainScreen].applicationFrame;

OR...

// Entire Screen
CGRect l_RectFrame = [UIScreen mainScreen].bounds;


MyView *l_aView = [[MyView alloc] initWithFrame:l_RectFrame];
...
Paul Brady
  • 503
  • 4
  • 10