iOS在故事板中切换嵌入式视图 [英] iOS Switching an embedded view in storyboard

查看:112
本文介绍了iOS在故事板中切换嵌入式视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力解决这个问题,我知道它应该可以完成,但是使用Objective-C而不是Appcelerator对iOS开发不熟悉我有新手问题。

I have been trying to figure this out all day and I know it should be able to be done, but being new to iOS development using Objective-C and not Appcelerator I'm having newbie issues.

我想要完成的是在我的其他视图中有一个嵌入式视图,但是能够通过编程切换嵌入哪个视图。我正在使用故事板。当我尝试使用视图容器时,它会显示附加的视图,但我无法将多个视图附加到故事板中的容器。我画了一张图片,但令我烦恼的是我不能在这里发布图片,因为我没有足够的rep点,所以我把它贴在我的网站上:
http://static.c6gx.com/images/iphone-embedded-views-diagram.jpg

What I am trying to accomplish is to have an embedded view in one of my other views, but to be able to switch which view is embedded through programming. I am using storyboards. When I tried to use the view container it displays the attached view, but I cannot attach multiple views to the container in the storyboard. I drew up an image, but to my annoyance I cannot post an image on here because I don't have enough "rep" points, so I posted it on my site: http://static.c6gx.com/images/iphone-embedded-views-diagram.jpg

稍后我希望在该视图容器中看起来像是推送segue,但首先我只想在嵌入式视图1,2,3等之间切换。我是应该使用视图容器来完成这个,还是其他类型的控制器?然后我如何调用转换来更改视图?

Later on I would like what appears to be a push segue, within that view container, but first I just want to be able to switch between embedded view 1, 2, 3, etc. Am I supposed to be using a view container to accomplish this, or some other type of controller? Then how would I call the transition to change the view?

感谢您提供任何帮助。

推荐答案

您可以像使用容器视图控制器一样切换视图控制器。我做了一个项目,我在其中添加了一个容器视图(实际上是2个,但我们只在这里处理一个),并将此代码添加到嵌入式控制器中,当您在容器视图中拖动时会自动获得该代码。我正在切换到的控制器NewRightController是一个UIViewController子类 - 在故事板中我将控制器的大小设置为Freeform,并更改了视图的大小以匹配嵌入式控制器视图的大小。这实际上并不影响视图的大小(它仍然以全屏显示),它只是更容易布局子视图。

You can switch the view controllers like you would do with a container view controller. I made a project where I added a container view (2 actually, but we're only dealing with one here), and added this code to the embedded controller that you get automatically when you drag in the container view. The controller I'm switching to, NewRightController, is a UIViewController subclass -- in the storyboard I set the controller's size to "Freeform", and changed the size of the view to match the size of the embedded controller's view. This doesn't actually affect the size of the view (it still logs as full screen), it just makes it easier to layout the subviews.

-(IBAction)switchToNewRight:(id)sender {
    UIView *rcView = [(ViewController *)self.parentViewController rightView]; // rcView is the right container view in my root view controller that self is embedded in.
    NewRightController *newRight = [self.storyboard instantiateViewControllerWithIdentifier:@"NewRight"];
    newRight.oldRightController = self; // pass self to new controller so I can come back to the same instance
    newRight.view.frame = rcView.bounds;
    [self.parentViewController addChildViewController:newRight];
    [self moveToNewController:newRight];
}

-(void)moveToNewController:(UIViewController *) newController {
    UIView *rcView = [(ViewController *)self.parentViewController rightView];
    [self willMoveToParentViewController:nil];
    [self.parentViewController transitionFromViewController:self toViewController:newController duration:1 options:UIViewAnimationOptionTransitionCurlUp animations:^{}
         completion:^(BOOL finished) {
             [self removeFromParentViewController];
             [rcView constrainViewEqual:newController.view];
             [newController didMoveToParentViewController:self];
         }];
}

我经过多次实验后发现,让观点成为正确的方法大小并在旋转后正确调整大小是为了将新控制器视图的帧初始设置为容器视图的边界,但是在过渡动画之后,向新视图添加约束,使其在旋转后保持大小。由于这段代码可能会被反复使用,我将它放在UIView上的一个类别中,使用一个方法constrainViewEqual:。以下是代码:

I found after much experimentation that the way to get the views to be the right size and to resize properly after rotation was to initially set the frame of the new controller's view to the container view's bounds, but then after the transition animation, add constraints to the new view that keep it sized right after rotation. Since this code might be used over and over, I put it in a category on UIView, with one method, constrainViewEqual:. Here is the code for that:

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

这篇关于iOS在故事板中切换嵌入式视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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