A dropdown view with Autolayout (and a few lines of code)

The aim of this post is just to explain how simple is to make a drowdown view controller using Autolayout (… and a few lines of code also). But believe be, the magic is done by Autolayout. At the end you will have a view controller that will allow to drop down the upper view:


Setup the project

Create a brand new Single View Application project:

Screen Shot 2015-12-23 at 13.46.09

Be sure that Language is swift:

Screen Shot 2015-12-23 at 13.49.35

In the default View controller add two views, one in the top-half and the other in the bottom-half. Finally add a button in the middle of two views:

Screen Shot 2015-12-23 at 14.04.24

I have added some color in the view components just for better clarification.


Top subview. Add the following 5 constraints:

Screen Shot 2015-12-23 at 14.07.19


Bottom subview. Add the following 4 constraints:

Screen Shot 2015-12-23 at 14.09.26


Button. Add the following 3 constraints:

Screen Shot 2015-12-23 at 14.12.02

Screen Shot 2015-12-23 at 14.13.52


Button – Bottom subview. For the following constraint do not forget select Top subview and Button.

Screen Shot 2015-12-23 at 14.15.52

And apply the following constraint:

Screen Shot 2015-12-23 at 14.18.58

Autolayout is giving to you a warning because he excepts that Button and Top subview would be bottom alligned.:

Screen Shot 2015-12-23 at 14.19.40

Our intention is leave the buttom in the middle of two views, so we have to update constraint:

Screen Shot 2015-12-23 at 14.25.10


… and a few lines of code



    @IBOutlet weak var btnFoldButton: UIButton!
    @IBOutlet weak var cnsUpperViewHeight: NSLayoutConstraint!

Now link outlets:

  • btnFoldButton to the button
  • cnsUpperViewHeight to the Top view height constraint.


Add pan gesture recognizer to Button:

 override func viewDidLoad() {


        self.btnFoldButton.addGestureRecognizer(UIPanGestureRecognizer.init(target: self, action: "panDetected:"))


What we do in the gesture recognizer is just calculate new value for cnsUpperViewHeight constraint, the hard work is performed by autolayout.

class ViewController:UIViewController {

var heightUpperView:CGFloat = 0.0


func panDetected(recognizer: UIPanGestureRecognizer){

        if(recognizer.state == UIGestureRecognizerState.Began){

            heightUpperView = self.cnsUpperViewHeight.constant


        let point:CGPoint = recognizer.translationInView(self.btnFoldButton.superview)

        let newHeight =  heightUpperView + point.y

        if(newHeight >= UIScreen.mainScreen().bounds.height * 0.15 &&

            newHeight <= UIScreen.mainScreen().bounds.height * 0.66){

                cnsUpperViewHeight.constant = newHeight





Let’s make some make up, just adding some picture at the top subview and a map at the bottom view.

For the picture and the map add the following constraints:

Screen Shot 2015-12-23 at 14.46.35


For the map do not forget the import MapKit in the view controller code:

Screen Shot 2015-12-23 at 14.53.20

Finally run the project:

Screen Shot 2015-12-23 at 14.56.24


Pan the button and you will see how top view is compressed, giving more room for bottom view.

You can also download the sample project from gitHub.


If what you really want is just shift the upper view and avoid compress effect on the top view, just remove the top constraint on the picture that is placed at the top subview:


Screen Shot 2015-12-23 at 15.03.23

This will be what you get:

Screen Shot 2015-12-23 at 15.04.26

Useful links:

Leave a Reply

Your email address will not be published. Required fields are marked *

10 + one =