How to add a UIViewController as a subview to a UIViewController(RootViewController)?

18,338

Solution 1

You should not make a UIViewController a subview of another UIViewController's view. What you likely want to do if treat the subview as a normal UIView (if not both of those views) so that you only have one UIViewController on screen and it occupies the entire screen.

More here: How to add an UIViewController's view as subview

Solution 2

Try to set frame to your enroll screen object then add it as a subview to loginview. Ex:

[enrollViewcontroller.view setFrame:CGRectMake(0,0,320,440)];
[self.view addsubview:enrollViewcontroller.view];
Share:
18,338
Pradeep Reddy Kypa
Author by

Pradeep Reddy Kypa

iOS Architect with a relevant experience of 13+ years. I want to master iOS SDK Technologies and very much interested to learn any new stuff regarding iOS.#SOreadytohelp

Updated on June 04, 2022

Comments

  • Pradeep Reddy Kypa
    Pradeep Reddy Kypa almost 2 years

    I have a UIViewController named LoginViewController, and it is a rootViewController in the AppDelegate. In the LoginViewController, I have two buttons: Login and Enroll.

    When I tap Login, I assign a TabBarController as the rootViewController, then show the TabBarController. However, now I think I need to add another UIViewcController as a subview when I tap Enroll. I tried the following:

    [self.view addsubview:viewcontroller.view];
    

    But the problem here is My ViewController's view.top is pinned about 20 pixels below the top of the screen. I think there is an issue with the status bar, but I can't figure out how to fix it.

    I think that I need to add my ViewController as a subview to the LoginViewController, then redirect from there to different views. Can someone please suggest other options?

  • Dee
    Dee almost 12 years
    change the frame according to your requirement. Because it is 20 pixel down try to change the y axis position like this CGRectMake(0,-20,320,440) and check.