How do I represent a view controller inside another in Swift?

I am making a searchable application. It will have a "Filter" button which, when clicked, will display a list of filtering options

The modal view controller should flow from right to left and not span the entire screen (as shown below).

I was able to programmatically render the view controller spanning the entire page, but I am having trouble figuring out how to animate the opening of the view controller (right to left), nor how to leave some space for see another view controller. It should be in Swift. I've seen a few questions about this, but all of the ones I could find used Objective C and legacy APIs.

thank

Example:

eBay app

+3


source to share


2 answers


There are undoubtedly several ways to do this.

One way would be to add a "container view" (this is a special kind of view in the list of objects in IB) to your current view controller and control - drag the inline segment from the container view to another scene of the view controller you want to accept. Place and size your container where you want it to end, and add a leading edge constraint that positions it at that position.

When you use the container view and embed the segue, the system takes care of all the household chores you need to do to place the child view controller.



Connect the output to the limitation.

In your viewWillAppear method, add screen width - container x

constraints to the constant and call layoutIfNeeded () on the content view to move it off-screen without animation. Store the amount added to the horizontal constraint in an instance variable.

Then when you want to animate it on the screen, change the constant value to the constraint (subtract the amount added to the viewWillAppear) and call it layoutIfNeeded()

inside the UIView block animateWithDuration:animations:

.

+7


source


You can simply create a container view and animate it from right to left on the screen.



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

      

0


source







All Articles