具有多个视图的 iOS 容器 [英] iOS Container with multiple views

查看:19
本文介绍了具有多个视图的 iOS 容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个包含容器的视图.在那个视图上,我想在表视图和地图视图之间切换.Yelp 的应用程序正是我想要做的.

I am trying to create a View that has a container in it. On that View, I want to switch between a table view and a map view. Yelp's app does exactly what I'm looking to do.

我遇到的问题是在视图之间切换.这是我的故事板的设置方式:

The problem I'm running into is switching between the views. Here is how my storyboard is set up:

我创建了一个控制器,用于在表格和地图之间切换.问题是如果我使用模态或自定义转场,它会接管整个视图.

I created a controller that is used to switch between the table and the map. The problem is if I use a modal or custom segue, it takes over the whole view.

我已经尝试将它们嵌入到一个确实有效的导航控制器中(我认为),但是一旦用户单击控制器中的某些内容,我需要将它们从导航控制器中取出并返回到主导航中.

I've tried embedding them in a navigation controller which does work (I think), but then once the user clicks on something in the controller I need to take them out of that navigation controller and back into the main navigation.

推荐答案

做到这一点的一种方法是将 SwitchViewController 设为自定义容器控制器,并将表视图或地图视图添加为其子视图.这是我之前使用过的一个示例,它的设置与您的故事板相同(我的 ContainerViewController 是您的 SwitchViewController,而我的标识符为InitialVC"和substituteVC"的控制器将是您的表视图和地图视图).这是我在 ContainerViewController 中的代码,

One way to do this, is to make the SwitchViewController a custom container controller, and add either the table view or map view as its child. Here is an example that I've used before that has the same set up as your storyboard (my ContainerViewController is your SwitchViewController, and my controllers with the identifiers "InitialVC" and "substituteVC" would be your table view and map view). This is code I have in the ContainerViewController,

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    UIViewController *initial = [self.storyboard instantiateViewControllerWithIdentifier:@"InitialVC"];
    [self addChildViewController:initial];
    [self.view addSubview:initial.view];
    self.currentController = initial;
    [self constrainViewEqual:self.currentController];
}

-(void)switchToNewView {
    UIViewController *sub = [self.storyboard instantiateViewControllerWithIdentifier:@"SubstituteVC"];
    [self addChildViewController:sub];
    sub.view.frame = self.view.bounds;
    [self moveToNewController:sub];
}

-(void)moveToNewController:(UIViewController *) newController {
    [self.currentController willMoveToParentViewController:nil];
    [self transitionFromViewController:self.currentController toViewController:newController duration:.6 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{}
         completion:^(BOOL finished) {
             [self.currentController removeFromParentViewController];
             [newController didMoveToParentViewController:self];
             self.currentController = newController;
             [self constrainViewEqual:self.currentController];
         }];
}

constrainViewEqual 是一种类别方法,我用它来设置视图的布局约束.看起来像这样,

constrainViewEqual is a category method that I use to set up the layout constraints for the views. It looks like this,

-(void)constrainViewEqual:(UIViewController *) vc {
    [vc.view setTranslatesAutoresizingMaskIntoConstraints:NO];
    NSLayoutConstraint *con1 = [NSLayoutConstraint constraintWithItem:self.view attribute:NSLayoutAttributeCenterX relatedBy:0 toItem:vc.view attribute:NSLayoutAttributeCenterX multiplier:1 constant:0];
    NSLayoutConstraint *con2 = [NSLayoutConstraint constraintWithItem:self.view attribute:NSLayoutAttributeCenterY relatedBy:0 toItem:vc.view attribute:NSLayoutAttributeCenterY multiplier:1 constant:0];
    NSLayoutConstraint *con3 = [NSLayoutConstraint constraintWithItem:self.view attribute:NSLayoutAttributeWidth relatedBy:0 toItem:vc.view attribute:NSLayoutAttributeWidth multiplier:1 constant:0];
    NSLayoutConstraint *con4 = [NSLayoutConstraint constraintWithItem:self.view attribute:NSLayoutAttributeHeight relatedBy:0 toItem:vc.view attribute:NSLayoutAttributeHeight multiplier:1 constant:0];
    NSArray *constraints = @[con1,con2,con3,con4];
    [self.view addConstraints:constraints];
}

我像这样从初始视图控制器(您标记为 ViewController 的那个)中的一个按钮调用 switchToNewView,

I call switchToNewView from a button in the initial view controller (the one you have labeled as ViewController) like this,

-(IBAction)doStuff:(id)sender {
    ContainerController *cc = (ContainerController *)self.childViewControllers[0];
    [cc switchToNewView];
}

这篇关于具有多个视图的 iOS 容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆