Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone: How to set UIViewController frame?

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?

like image 299
Nic Hubbard Avatar asked Feb 08 '11 04:02

Nic Hubbard


People also ask

What is UIViewController in IOS?

The UIViewController class defines the shared behavior that's common to all view controllers. You rarely create instances of the UIViewController class directly. Instead, you subclass UIViewController and add the methods and properties needed to manage the view controller's view hierarchy.

What is the difference between UIView and UIViewController?

They are separate classes: UIView is a class that represents the screen of the device of everything that is visible to the viewer, while UIViewController is a class that controls an instance of UIView, and handles all of the logic and code behind that view.

What is UIViewController life cycle?

The LifecycleThe view controller lifecycle can be divided into two big phases: the view loading and the view lifecycle. The view controller creates its view the first time the view is accessed, loading it with all the data it requires. This process is the view loading.


2 Answers

@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.

like image 193
Robin Avatar answered Nov 16 '22 02:11

Robin


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.

like image 26
Chris Zelenak Avatar answered Nov 16 '22 00:11

Chris Zelenak