具有多个 View 的 iOS 容器

标签 ios storyboard

我正在尝试创建一个其中包含容器的 View 。在该 View 上,我想在 TableView 和 map View 之间切换。 Yelp 的应用程序正是我想要做的。

我遇到的问题是 View 之间的切换。以下是我的 Storyboard的设置方式:

Storyboard

我创建了一个 Controller ,用于在表格和 map 之间切换。问题是,如果我使用模态或自定义转场,它会接管整个 View 。

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

最佳答案

实现此目的的一种方法是使 SwitchViewController 成为自定义容器 Controller ,并将 TableView 或 map View 添加为其 subview 。这是我之前使用过的一个示例,它与您的 Storyboard具有相同的设置(我的 ContainerViewController 是您的 SwitchViewController,而我的带有标识符“InitialVC”和“substituteVC”的 Controller 将是您的 TableView 和 map View )。 这是我在 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 是一种类别方法,我用它来设置 View 的布局约束。看起来像这样,

-(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];
}

我从初始 View Controller (您标记为 ViewController 的那个)中的按钮调用 switchToNewView,如下所示,

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

关于具有多个 View 的 iOS 容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23318709/

相关文章:

ios - 向类的实例发送 "class"消息是什么意思

ios - 如何在我的应用程序 iPhone 中通过 fbconnect 获取用户的 facebook 个人资料图片?

ios - 是否可以将约束从一个 View 复制到另一个 View ?

ios - 使用大小类更改字体大小

ios - 单个 Storyboard上的所有 View Controller

ios - 无法在自定义 UIViewController 中分配自定义 UITableViewCell

iphone - TTPhotoViewController:停用旋转(无方向横向)

ios - 解析错误预期标识符或 '('

android - Titanium 项目中 android 中的 TableViewRow

ios - Xcode 7 中的对象库未显示所有可能的对象