Coy*_*te6 7 views storyboard ios
我一直试图弄明白这一点,我知道它应该可以完成,但是使用Objective-C而不是Appcelerator对iOS开发不熟悉我有新手问题.
我想要完成的是在我的其他视图中有一个嵌入式视图,但是能够通过编程切换嵌入哪个视图.我正在使用故事板.当我尝试使用视图容器时,它会显示附加的视图,但我无法将多个视图附加到故事板中的容器.我画了一张图片,但令我烦恼的是我不能在这里张贴图片,因为我没有足够的"rep"点,所以我把它贴在我的网站上:http: //static.c6gx.com/images/iphone -嵌入式的视图,diagram.jpg
后来我希望看起来像是一个推送segue,在那个视图容器中,但首先我只想在嵌入式视图1,2,3等之间切换.我应该使用一个视图容器来完成这个还是其他类型的控制器?然后,我将如何调用转换来更改视图?
感谢您提供任何帮助.
您可以像使用容器视图控制器一样切换视图控制器.我创建了一个项目,我在其中添加了一个容器视图(实际上是2个,但我们只在这里处理一个),并将此代码添加到嵌入式控制器中,当您在容器视图中拖动时会自动获得该代码.我正在切换到的控制器NewRightController是一个UIViewController子类 - 在故事板中我将控制器的大小设置为"Freeform",并更改了视图的大小以匹配嵌入式控制器视图的大小.这实际上并不影响视图的大小(它仍然以全屏显示),它只是更容易布局子视图.
-(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];
}];
}
Run Code Online (Sandbox Code Playgroud)
我经过多次实验后发现,将视图调整为正确大小并在旋转后正确调整大小的方法是初始将新控制器视图的框架设置为容器视图的边界,但是在转换动画之后,添加约束旋转后立即调整大小的新视图.由于这段代码可能会被反复使用,我将它放在UIView上的一个类别中,使用一个方法constrainViewEqual:.这是代码:
-(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];
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10607 次 |
| 最近记录: |