Ancinek Ancinek - 1 year ago 49
Objective-C Question

Adding view from UIViewController

I created my own freeform first view controller and placed a table view inside of it. I set its class properly and created an IBOutlet for this table.

In my second view controller I want to display the view from first view controller as a slide in window, so I created and IBAction that will do this:

- (IBAction)optionsButtonTapped:(id)sender {
OptionsViewController *optionsVC = [[OptionsViewController alloc] init];

// Custom CGRect:
CGFloat width = self.view.frame.size.width - 10.0;
CGFloat height = [optionsVC.optionsTableView numberOfRowsInSection: 0] * 50.0;
CGFloat x = self.view.frame.origin.x + ((self.view.frame.size.width - width) / 2);
CGFloat y = self.view.frame.origin.y + self.view.frame.size.height + (1.2 * height);
CGRect hiddenFrame = CGRectMake(x, y, width, height);

// Custom UIView:
UIView *view = optionsVC.view;
view.frame = hiddenFrame;
[self.view addSubview: view];
[self addChildViewController: optionsVC];
[optionsVC didMoveToParentViewController: self];
//NSLog(@"Frame for the view is: %@", NSStringFromCGRect(hiddenFrame));

// Animation:
[UIView animateWithDuration: 0.3 animations:^(void){
CGRect visibleFrame = hiddenFrame;
visibleFrame.origin.y = self.view.frame.origin.y + self.view.frame.size.height - 5.0 - height;
view.frame = visibleFrame;
} completion:^(BOOL finished) {
NSLog(@"Animation completed");

The height when I use NSLog is 0, that is because the tableview is not being loaded when I init optionsVC (I set up the dataSource and delegate). Even if I set the height to eg. 200.0 the view still does not appear, however I have a log from second view controller that viewDidLoad was performed.

What am I doing wrong?

Answer Source

You should use ContainerView to show Controller inside controller. You can animate its position and use delegation to communicate with each other.

Maybe You should consider other design for Your app (please, add how You see Your UI because it is not clear for me), You should avoid using ContainerView. Refer to Implementing a Container View Controller