如何在Swift中向另一个内部呈现视图控制器?

时间:2023-01-23 23:44:02

I am making a searchable list app. It will have a 'Filter' button that when clicked, will display a list of filtering options

我正在制作一个可搜索的列表应用程序。它将有一个“过滤器”按钮,单击该按钮将显示过滤选项列表

The modal view controller should come from the right to left, and not cover the whole screen (as seen below).

模态视图控制器应该从右到左,而不是覆盖整个屏幕(如下所示)。

I have managed to display a view controller programmatically, covering the whole page, but I can't manage to find out how to animate the opening of the view controller (right to left) as well as how to leave some space to see the other View Controller. It must be in Swift. I have seen a few questions about this but all the ones I could find used Objective C and deprecated APIs.

我已经成功地以编程方式显示了一个视图控制器,覆盖了整个页面,但是我无法找到如何为视图控制器的开口设置动画(从右到左)以及如何留出一些空间来查看另一个查看控制器。它一定是在Swift中。我已经看到了一些关于这个的问题,但是我能找到的所有问题都使用了Objective C和不推荐使用的API。

Thanks

Example:

如何在Swift中向另一个内部呈现视图控制器?

2 个解决方案

#1


7  

There are no doubt multiple ways to do this.

毫无疑问,有多种方法可以做到这一点。

One way would be to add a "container view" (It's a special view type in the list of objects in IB) into your current view controller and control-drag an embed segue from the container view onto the other view controller scene that you want to host. Place and size your container view where you want it to end up, and add a leading edge constraint that positions it at that position.

一种方法是将“容器视图”(它是IB中对象列表中的特殊视图类型)添加到当前视图控制器中并控制 - 将嵌入segue从容器视图拖动到您想要的其他视图控制器场景举办。放置容器视图并将其放大到最终位置,并添加将其定位在该位置的前沿约束。

When you use a container view and an embed segue the system takes care of all the housekeeping you need to do in order to host a child view controller.

当您使用容器视图和嵌入segue时,系统会处理您需要执行的所有内务处理,以便托管子视图控制器。

Wire an outlet to the constraint.

将插座连接到约束。

In your viewWillAppear method, add screen width - container x to the constraint's constant and call layoutIfNeeded() on the content view to move it off-screen without animation. Save the amount you added to the horizontal constraint in an instance variable.

在viewWillAppear方法中,将屏幕宽度 - 容器x添加到约束的常量,并在内容视图上调用layoutIfNeeded(),将其移出屏幕而不显示动画。将添加的数量保存到实例变量中的水平约束中。

Then when you want to animate it onto the screen change the constant value on the constraint (subtract the amount you added in viewWillAppear) and call layoutIfNeeded() inside a UIView animateWithDuration:animations: block.

然后,当您想要将其设置到屏幕上时,更改约束上的常量值(减去在viewWillAppear中添加的量)并在UIView animateWithDuration:animations:block中调用layoutIfNeeded()。

#2


0  

You could just create a container view and animate that onscreen from right to left.

您可以创建一个容器视图,并从右到左为屏幕上的动画设置动画。

let containerView = UIView()
    containerView.addSubview(viewController.view)

#1


7  

There are no doubt multiple ways to do this.

毫无疑问,有多种方法可以做到这一点。

One way would be to add a "container view" (It's a special view type in the list of objects in IB) into your current view controller and control-drag an embed segue from the container view onto the other view controller scene that you want to host. Place and size your container view where you want it to end up, and add a leading edge constraint that positions it at that position.

一种方法是将“容器视图”(它是IB中对象列表中的特殊视图类型)添加到当前视图控制器中并控制 - 将嵌入segue从容器视图拖动到您想要的其他视图控制器场景举办。放置容器视图并将其放大到最终位置,并添加将其定位在该位置的前沿约束。

When you use a container view and an embed segue the system takes care of all the housekeeping you need to do in order to host a child view controller.

当您使用容器视图和嵌入segue时,系统会处理您需要执行的所有内务处理,以便托管子视图控制器。

Wire an outlet to the constraint.

将插座连接到约束。

In your viewWillAppear method, add screen width - container x to the constraint's constant and call layoutIfNeeded() on the content view to move it off-screen without animation. Save the amount you added to the horizontal constraint in an instance variable.

在viewWillAppear方法中,将屏幕宽度 - 容器x添加到约束的常量,并在内容视图上调用layoutIfNeeded(),将其移出屏幕而不显示动画。将添加的数量保存到实例变量中的水平约束中。

Then when you want to animate it onto the screen change the constant value on the constraint (subtract the amount you added in viewWillAppear) and call layoutIfNeeded() inside a UIView animateWithDuration:animations: block.

然后,当您想要将其设置到屏幕上时,更改约束上的常量值(减去在viewWillAppear中添加的量)并在UIView animateWithDuration:animations:block中调用layoutIfNeeded()。

#2


0  

You could just create a container view and animate that onscreen from right to left.

您可以创建一个容器视图,并从右到左为屏幕上的动画设置动画。

let containerView = UIView()
    containerView.addSubview(viewController.view)