Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To Add A SubViewController In The Middle Of The MainViewController

I want to add a subviewcontroller in the main viewcontroller, but I just want that covers one section, not all the screen. I want it in the middle of the screen. And also, want to know if I can make this subviewcontroller transparent, so I still can see the image background of my main viewcontroller. Currently, I have my mainviewcontroller and I add another viewcontroller using this way:

MenuAllEntriesViewController *menuAE = [[MenuAllEntriesViewController alloc] init];
[menuAE setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[self presentModalViewController:menuAE animated:YES];

But my subviewcontroller covers all the screen... Thank you

like image 404
user1600801 Avatar asked Dec 04 '25 17:12

user1600801


1 Answers

To do this programmatically: First add an empty UIView to your main view. Also create an IBOutlet from your MainWindowController to the view. This will be the container view of your child view controller. Just set its size and position to where you want to display child view.

Then in your MainWindowController.m you can use the following code to add a child view controller whose view is being displayed in the specified view within your main view:

//keep a strong reference to the view controller
self.otherViewController = [[OtherViewController alloc] init]; 
[self addChildViewController:otherViewController];
//set the frame
CGFloat width = containerView.frame.size.width;
CGFloat height = containerView.frame.size.height;
self.otherViewController.view.frame = CGRectMake(0, 0, width, height);
[containerView addSubview:self.otherViewController.view];
[self.otherViewController didMoveToParentViewController:self];

If you use Storyboards and target iOS 6, you don't even need to do any programming. In the Storyboard view (formerly called Interface Builder) you can just drop a Container View from the Objects Library to your main view. Interface Builder will automatically create a child view controller but you can also point the containment segue to any other view controller.

like image 56
codingFriend1 Avatar answered Dec 07 '25 05:12

codingFriend1