繁体   English   中英

具有多个视图的iOS容器

[英]iOS Container with multiple views

我正在尝试创建一个具有容器的视图。 在该视图上,我想在表视图和地图视图之间切换。 Yelp的应用程序完全可以满足我的需求。

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

故事板

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

我曾尝试将它们嵌入可正常工作的导航控制器中(但我认为),但是一旦用户单击控制器中的某些内容,我就需要将其从该导航控制器中取出并返回到主导航中。

一种方法是使SwitchViewController成为自定义容器控制器,然后将表视图或地图视图添加为其子级。 这是我之前使用的示例,该示例与情节提要相同(我的ContainerViewController是SwitchViewController,标识为“ InitialVC”和“ substituteVC”的控制器将是您的表视图和地图视图)。 这是我在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是一种分类方法,我用于设置视图的布局约束。 看起来像这样

-(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,

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

您可以将新的详细信息viewControllers添加到情节提要中,并配置它们的大小以匹配界面构建器中的容器

在实用程序中选择大小-自由格式

然后

在这里您可以指定尺寸

从您的切换视图控制器和新视图控制器进行的Segue仅应占用该视图的大小。

我希望这有帮助,

干杯,

吉姆

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM