cross
cross

Reputation: 363

What is the best way to develop applications using the auto layout in Xcode?

So I guess this is going to be closed for being too subjective and too opinion based but if anyone can help me I would appreciate it.

I got a question. If I have a few controllers that all have almost the same thing For example they have the same background, have a menu going around the edge but the actual content is different. I had a couple of ideas. 1) Just have one view controller and just kill the objects for that current view if the user chooses a different option on the menu and spawn the new objects for that menu. My issue with this way was that I could't find a way to use the auto layout with this.

Second way would to be have a function in a .swift file that I can call and it creates an image view and sets up the menu an everything like that. I have the opposite issue here though, now the auto layout won't work.

App devs must have a way of doing this, I'm just probably thinking of this completely the wrong way.

Is there a better way to be doing this - I am sure there is? I would appreciate it if someone could point me in the correct direction.

Thanks

EDIT:

I should make it clear that the language I am using is swift.

Upvotes: 0

Views: 151

Answers (1)

dev
dev

Reputation: 2200

You can create custom container view controller and swap the view controllers for the part that change according to the user selection.

--Adding Example--

e.g iPad's Settings app. The left side is a table view and right side is detail view which changes on user selection. So Tableview can be wrapped in a view controller let's say ListViewController. This will not change. The right side will be DetailViewController which would be swapped according to user selection. Your ContainerViewController will have 2 view controllers at all times.

Here is how to add view controllers as child and set their views in objective-c.

 - (void) setupContentViewControllerWith: (DetailViewController*) detailViewController andListViewController:(ListViewController*)listViewController {
    [self addChildViewController:listViewController];
    [self addChildViewController:detailViewController];
    listViewController.view.frame = CGRectMake(kListView_X, kListView_Y, kListView_Width, kListView_Height);
    detailViewController.view.frame = CGRectMake(kListView_Width, kDetailView_Y, self.view.bounds.size.width, self.view.bounds.size.height-kDetailView_Y);
    [self.scrollContainer addSubview:listViewController.view];
    [self.scrollContainer addSubview:detailViewController.view];
    [self.scrollContainer setContentSize:CGSizeMake(kListView_Width+self.view.bounds.size.width, self.view.bounds.size.height)];
}

When user selects new item from the list, you can swap DetailViewControllers as below

 - (void) replaceEpisodeControllerWith:(DetailViewController *)detailViewController {
detailViewController.view.frame = CGRectMake(kListView_Width, kDetailView_Y,  self.view.bounds.size.width, self.view.bounds.size.height-kDetailView_Y);
[UIView transitionFromView:currentDetailViewController.view
                    toView:detailViewController.view
                  duration:0.0
                   options:UIViewAnimationOptionTransitionNone
                completion:^(BOOL finished)
 {
     [currentDetailViewController.view removeFromSuperview];
     [currentDetailViewController removeFromParentViewController];
     [currentDetailViewController release];
     currentDetailViewController = detailViewController;
 }];
}

I don't have swift version of this.

Upvotes: 1

Related Questions