xcode 使用 Storyboard 在同一个容器视图中的多个视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26967213/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 06:11:09  来源:igfitidea点击:

Multiple Views in Same Container View Using Storyboard

iosobjective-ciphonexcodestoryboard

提问by Kiran Thapa

I have a viewcontroller with a background image and title header. This view remains same most of the time. And I have different child views that i will be showing and hiding inside this viewcontroller.

我有一个带有背景图像和标题标题的视图控制器。大多数时候,这种观点保持不变。我有不同的子视图,我将在这个视图控制器中显示和隐藏它们。

How can i achieve this using storyboard?

我如何使用故事板实现这一目标?

  1. Do i have to create separate xibs for each child views?
  2. Or do i create individual views inside the same view controller and hide and show them as needed?
  1. 我是否必须为每个子视图创建单独的 xib?
  2. 或者我是否在同一个视图控制器中创建单独的视图并根据需要隐藏和显示它们?

Or is there any other elegant solution?

或者还有其他优雅的解决方案吗?

Thanks.

谢谢。

回答by Amit Thakur

Please have a look at this tutorial: How to use a 'Container View' in iOS?

请查看本教程:如何在 iOS 中使用“容器视图”?

The overall idea is shown in the image.

总体思路如图所示。

enter image description here

在此处输入图片说明

回答by Alex Andrews

It will be much easy to design your UI if you use container views.

如果您使用 container views.

Add the container viewfor all your child views. Create propertiesfor all the child container viewsin your main View Controller. Design the child views with in the container view controller. Apply business logic in your main view controller to show and hide the child views accordingly.

container view为所有子视图添加。propertiescontainer viewsmain 中的所有孩子创建View Controller。在容器中设计子视图view controller。在主view controller 中应用业务逻辑以相应地显示和隐藏子视图。

回答by LoGoCSE

It will be better when add and remove subviews instead of hiding and showing.

添加和删​​除子视图而不是隐藏和显示会更好。

In same ViewControlleryou can use addSubView:to add and removeFromSuperViewto remove views

同样,ViewController您可以使用addSubView:添加和removeFromSuperView删除视图

回答by Jiaru

Generally, you don't need to create separate xibs for each child views (technically, we can draw all view controllers on the same storyboard).

通常,您不需要为每个子视图创建单独的 xib(从技术上讲,我们可以在同一个故事板上绘制所有视图控制器)。

With the hide and show issue, make the child views as IBOutlet, and then let your code to decide which view should show and which one should hide.

对于隐藏和显示问题,将子视图设为 IBOutlet,然后让您的代码决定应该显示哪个视图以及应该隐藏哪个视图。

回答by Prajeet Shrestha

Adding and removing subviews is the best way to achieve this. But if you do this with storyboard, all the overlapping views could look messy and will be hard to manage.

添加和删​​除子视图是实现这一目标的最佳方式。但是如果你用故事板来做这件事,所有重叠的视图可能看起来很混乱并且很难管理。

The best way to achieve this is adding views programmatically, and also setting layout constraints programatically.

实现此目的的最佳方法是以编程方式添加视图,并以编程方式设置布局约束。

UIView *subView = [[UIView alloc]init];
subView.backgroundColor = [UIColor blackColor];
NSDictionary *viewsDictionary = @{@"subView":subView};

NSDictionary *metrics = @{@"offset":@20};

NSArray *heightConstraint = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-offset-[subView]-offset-|"
                                                                    options:0
                                                                    metrics:metrics
                                                                      views:viewsDictionary];

NSArray *widthConstraint = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-offset-[subView]-offset-|"
                                                                   options:0
                                                                   metrics:metrics
                                                                     views:viewsDictionary];
[self.view addConstraints:heightConstraint];
[self.view addConstraints:widthConstraint];

The code above for example will add the subview whish has top,bottom,leading and trailing space set to 20 from the main view of the view controller.

例如,上面的代码将从视图控制器的主视图添加顶部、底部、前导和尾随空间设置为 20 的子视图。