1

Hi i am trying to add a view controller as a child view. and later remove this view controller form parent view.I am using following code for this purpose.

 self.loginView = [self.storyboard instantiateViewControllerWithIdentifier:@"LOGIN"];
 [self.view addSubview:self.loginView.view];

This code works fine for iOS8 but in iOS7 this code is not working it shows the half of the screen.On half part login is shown.

What could be the solution for this??

0

2 Answers 2

10

Add a custom UIView object in your main view (in XIB) in which you want to add and show your child view controller. Let contentView is the name of that view. Use following code to add child view controller:

self.loginView = [self.storyboard instantiateViewControllerWithIdentifier:@"LOGIN"];
[self addChildViewController:self.loginView];
[self.loginView.view setFrame:CGRectMake(0.0f, 0.0f, self.contentView.frame.size.width, self.contentView.frame.size.height)];
[self.contentView addSubview:self.loginView.view];
[self.loginView didMoveToParentViewController:self]; 

if you don't add last line, your child view controller will not receive events. By using this code you can simultaneously receive events in both parent and child view controllers.

Sign up to request clarification or add additional context in comments.

3 Comments

I have a question for you guys. how can I pass a data (string or int) to child view before or after loading the childView. Thank you..
I don't really understand why you edit the post, this code is unreadable. You are calling a UIViewController view, it make the code very confusing. I hope you are not coding like this in real life.
I had used the name same as what was mentioned in question. I never use these naming convention in real life.. :)
0

Adding a UIViewController as a subView is not standard programming I believe, UIViewController is intended to handle a complete view on screen. Instead of adding a UIViewController to your UIView you should create a custom UIView and then use that UIView to achieve whatever results you want to.

EDIT : Use XIB to create a custom UIView.

2 Comments

I have designed the view from story board so that i why i am adding a view controller as subview
Yes but you can create custom Views in Xibs in the same way you did for Storyboards. You just have to assign a UIView class to the xib and then create an instance of that class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.