EmbeddedSwapping
Demonstration of how to make a custom container view controller manage multiple child view controllers using storyboards.
Install / Use
/learn @mluton/EmbeddedSwappingREADME
Demonstration of how to make a custom container view controller manage multiple child view controllers using storyboards. This solution is heavily based on Peregrin Planet's Container View Controllers in the Storyboard.
The child view controllers are connected to their container with a custom segue. The custom segue doesn't do anything but exists for the purpose of connecting things together in the storyboard. The custom container view controller manages the child view controllers in prepareForSegue:sender.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:SegueIdentifierFirst])
{
if (self.childViewControllers.count > 0) {
[self swapFromViewController:[self.childViewControllers objectAtIndex:0] toViewController:segue.destinationViewController];
}
else {
[self addChildViewController:segue.destinationViewController];
((UIViewController *)segue.destinationViewController).view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
[self.view addSubview:((UIViewController *)segue.destinationViewController).view];
[segue.destinationViewController didMoveToParentViewController:self];
}
}
else if ([segue.identifier isEqualToString:SegueIdentifierSecond])
{
[self swapFromViewController:[self.childViewControllers objectAtIndex:0] toViewController:segue.destinationViewController];
}
}
- (void)swapFromViewController:(UIViewController *)fromViewController toViewController:(UIViewController *)toViewController
{
toViewController.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
[fromViewController willMoveToParentViewController:nil];
[self addChildViewController:toViewController];
[self transitionFromViewController:fromViewController toViewController:toViewController duration:1.0 options:UIViewAnimationOptionTransitionCrossDissolve animations:nil completion:^(BOOL finished) {
[fromViewController removeFromParentViewController];
[toViewController didMoveToParentViewController:self];
}];
}
See the blog post for a more detailed description. Download the full project for the complete solution. Comments, Feedback, Suggestions: Michael Luton
MIT license.
Related Skills
node-connect
347.0kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
107.8kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
347.0kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
347.0kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
