我有一个主视图控制器,在这个视图控制器中我将另一个视图控制器显示为其子视图控制器。显示子视图控制器的代码如下所示。 self.currentController是子控制器,它将位于主控制器内。
self.addChildViewController(self.currentController!)
self.currentController?.view.frame = self.operationView.bounds
self.currentController?.view.layoutIfNeeded()
self.operationView.addSubview((self.currentController?.view!)!)
self.setNeedsStatusBarAppearanceUpdate()
现在我想通过使用下面的代码在子视图控制器中执行show segue(另一个控制器,让我们称之为ThirdController):
performSegueWithIdentifier("ShowSegue", sender: nil)
这样做,ThirdController将填满全屏。我想要做的是在子控制器位置显示第三个控制器。我怎么能这样做?
答案 0 :(得分:2)
好的,抱歉,我不知道如何在Swift中写出答案。所以我将向您展示我的解决方案是如何在Objective-C中完成的。
加载第一个子视图的代码:
- (void)loadASubview
{
subview = [self.storyboard instantiateViewControllerWithIdentifier:@"FirstView"];
[self addChildViewController:subview];
[self.view addSubview:subview.view];
[subview didMoveToParentViewController:self];
[subview.view setFrame:self.view.bounds];
}
卸载子视图的代码:
- (void)unloadASubview
{
[subview willMoveToParentViewController:nil];
[subview.view removeFromSuperview];
[subview removeFromParentViewController];
}
最初,当我需要加载子视图A时,我只需调用 loadASubview 。之后,如果我需要加载另一个子视图,我将在加载新子视图之前通过调用unloadASubview卸载我之前加载的子视图。
请注意,函数内的“subview”变量在外部声明。
我希望这会对你有所帮助。
答案 1 :(得分:0)
Swift 4
func loadASubView(){
subview = self.storyboard?.instantiateViewController(withIdentifier: "childviewstoryboardid")
self.addChildViewController(subview!)
self.containerView.addSubview(subview!.view)
subview?.didMove(toParentViewController: self)
subview?.view.frame = self.containerView.frame
}
func unloadASubview(){
subview?.willMove(toParentViewController: nil)
subview?.view.removeFromSuperview()
subview?.removeFromParentViewController()
}
答案 2 :(得分:0)
这是我对autoLayout的决心。这是无需进行任何额外修改的最佳方法。
在Children View Controller中,通常可以使用此功能“ show(vc:UIViewController,sender:Any?)” 来显示viewController(如果具有navigationController,它将推送新的Controller ,也不展示它)
Swift 4.2
func setChild() {
let vc = UIStoryboard(name: "YOUR_STORYBOARD_NAME", bundle: nil).instantiateViewController(withIdentifier: "YOUR_CHILD_ID")
self.addChild(vc)
// need to set translatesAutoresizingMaskIntoConstraints false to enable auto layout
vc.view.translatesAutoresizingMaskIntoConstraints = false
/// add subview to parent
view.addSubview(vc.view)
/// send subview to back for show other view in parent
view.sendSubviewToBack(vc.view)
/// call did move to parent to show current children
vc.didMove(toParent: self)
/// <---create auto layout with VFL--->
let horiConstraints = NSLayoutConstraint.constraints(withVisualFormat: "H:|-0-[view]-0-|",
options: .alignAllCenterY,
metrics: nil,
views: ["view" : vc.view])
let verticalConstraints = NSLayoutConstraint.constraints(withVisualFormat: "V:|-0-[view]-0-|",
options: .alignAllCenterX,
metrics: nil,
views: ["view" : vc.view])
view.addConstraints(horiConstraints)
view.addConstraints(verticalConstraints)
view.layoutIfNeeded()
}
之后,您可以删除第一个孩子
func removeFirstChild() {
children.first?.willMove(toParent: nil)
children.first?.view.removeFromSuperview()
children.first?.didMove(toParent: nil)
}
或从父母中删除所有孩子
func removeAllChild() {
for child in children {
child.willMove(toParent: nil)
child.view.removeFromSuperview()
child.didMove(toParent: nil)
}
}
Object-C
- (void)setChild {
UIViewController *vc = [[UIStoryboard storyboardWithName:@"YOUR_STORYBOARD_NAME" bundle:nil] instantiateViewControllerWithIdentifier:@"YOUR_CHILD_ID"];
[self addChildViewController:vc];
// need to set translatesAutoresizingMaskIntoConstraints false to enable auto layout
vc.view.translatesAutoresizingMaskIntoConstraints = NO;
/// add subview to parent
[self.view addSubview:vc.view];
/// send subview to back for show other view in parent
[self.view sendSubviewToBack:vc.view];
/// call did move to parent to show current children
[vc didMoveToParentViewController:self];
/// <---create auto layout with VFL--->
NSArray<__kindof NSLayoutConstraint *> *horiConstraints;
horiConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[view]-0-|"options:NSLayoutAttributeCenterY
metrics:nil
views:@{@"view": vc.view}];
NSArray<__kindof NSLayoutConstraint *> *verticalConstraints;
verticalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[view]-0-|"
options:NSLayoutAttributeCenterX
metrics:nil
views:@{@"view": vc.view}];
[self.view addConstraints:horiConstraints];
[self.view addConstraints:verticalConstraints];
[self.view layoutIfNeeded];
}
之后,您可以删除第一个孩子
- (void)removeFirstChild {
UIViewController *child = self.childViewControllers.firstObject;
[child willMoveToParentViewController:nil];
[child.view removeFromSuperview];
[child didMoveToParentViewController:nil];
}
或从父母中删除所有孩子
- (void)removeAllChild {
for (UIViewController* child in self.childViewControllers) {
[child willMoveToParentViewController:nil];
[child.view removeFromSuperview];
[child didMoveToParentViewController:nil];
}
}