         
         // can't touch top
         
-//        var cantTouchTopConstraint = NSLayoutConstraint(
-//            item: self.popup,
-//            attribute: directionToAttribute[dir]!,
-//            relatedBy: NSLayoutRelation.GreaterThanOrEqual,
-//            toItem: self.superview,
-//            attribute: directionToAttribute[dir]!,
-//            multiplier: 1,
-//            constant: 2) // TODO: layout
-//        cantTouchTopConstraint.priority = 1000
-//        self.constraintStore += (self.superview, cantTouchTopConstraint)
+        var cantTouchTopConstraint = NSLayoutConstraint(
+            item: self.popup,
+            attribute: directionToAttribute[dir]!,
+            relatedBy: (dir == Direction.Right ? NSLayoutRelation.LessThanOrEqual : NSLayoutRelation.GreaterThanOrEqual),
+            toItem: self.superview,
+            attribute: directionToAttribute[dir]!,
+            multiplier: 1,
+            constant: 2) // TODO: layout
+        cantTouchTopConstraint.priority = 1000
+        self.constraintStore += (self.superview, cantTouchTopConstraint)
+        
+        if dir.horizontal() {
+            var cantTouchTopConstraint = NSLayoutConstraint(
+                item: self.popup,
+                attribute: directionToAttribute[Direction.Up]!,
+                relatedBy: NSLayoutRelation.GreaterThanOrEqual,
+                toItem: self.superview,
+                attribute: directionToAttribute[Direction.Up]!,
+                multiplier: 1,
+                constant: 5) // TODO: layout
+            cantTouchTopConstraint.priority = 1000
+            self.constraintStore += (self.superview, cantTouchTopConstraint)
+        }
+        else {
+            var cantTouchSideConstraint = NSLayoutConstraint(
+                item: self.superview,
+                attribute: directionToAttribute[Direction.Right]!,
+                relatedBy: NSLayoutRelation.GreaterThanOrEqual,
+                toItem: self.popup,
+                attribute: directionToAttribute[Direction.Right]!,
+                multiplier: 1,
+                constant: 17) // TODO: layout
+            cantTouchSideConstraint.priority = 1000
+            var cantTouchSideConstraint2 = NSLayoutConstraint(
+                item: self.superview,
+                attribute: directionToAttribute[Direction.Left]!,
+                relatedBy: NSLayoutRelation.LessThanOrEqual,
+                toItem: self.popup,
+                attribute: directionToAttribute[Direction.Left]!,
+                multiplier: 1,
+                constant: 17) // TODO: layout
+            cantTouchSideConstraint2.priority = 1000
+            self.constraintStore += (self.superview, cantTouchSideConstraint)
+            self.constraintStore += (self.superview, cantTouchSideConstraint2)
+        }
         
         // centering
         
-        let centerConstraint = NSLayoutConstraint(
+        var centerConstraint = NSLayoutConstraint(
             item: self.keyView,
             attribute: (dir.horizontal() ? NSLayoutAttribute.CenterY : NSLayoutAttribute.CenterX),
             relatedBy: NSLayoutRelation.Equal,

            self.popupDirection = Direction.Up
            self.setupPopupConstraints(self.popupDirection)
            self.configurePopup(self.popupDirection)
            
//            if self.popup!.frame.height < 20 {
//            NSLog("popup frame is \(NSStringFromCGRect(self.popup!.frame))")
            
            var popupPosition = self.superview.convertPoint(self.popup!.frame.origin, fromView: self)
            popupPosition.y = popupPosition.y - self.popup!.frame.height
            NSLog("popup positio is \(NSStringFromCGPoint(popupPosition))")
            
            if popupPosition.y < 13 {
                if self.frame.origin.x < self.superview.bounds.width/2 {
                    self.popupDirection = Direction.Right
                }
                else {
                    self.popupDirection = Direction.Left
                }
                
                self.setupPopupConstraints(self.popupDirection)
                self.configurePopup(self.popupDirection)
                
                super.layoutSubviews()
                
                var popupPosition = self.superview.convertPoint(self.popup!.frame.origin, fromView: self)
                popupPosition.y = popupPosition.y - self.popup!.frame.height
                NSLog("popup positio is \(NSStringFromCGPoint(popupPosition))")
            }
        }
        

            attribute: (dir.horizontal() ? NSLayoutAttribute.CenterY : NSLayoutAttribute.CenterX),
            multiplier: 1,
            constant: 0)
        centerConstraint.priority = 500
        self.constraintStore += (self, centerConstraint)
        
        for (view, constraint) in self.constraintStore {
