I have a UIView that takes up bottom half of the screen. It has a row of buttons on the top and then a UITableView that takes up the rest of the UIView. I want users to be able to click the button labeled "drag me" and be able to pull the UIView up to cover as much of the top half of the screen as they want.
More info on layout of UITableView and the UIView that contains it.
- The UIView and the and UITableView are positioned using Auto Layout.
- The UIView is pinned to a small UIView at bottom of screen.
- The UITableView is pinned to the bottom of the UIView that contains it
I have successfully gotten the drag to increase or decrease size of UIView. By doing this it also moves the position of everything that it contains (ie top row of buttons and UITableView). I did by changing its frame (although originally positioned using Auto Layout) according to the translation of the y coordinate of the drag:
containerView.frame = CGRectMake(containerView.frame.minX, containerView.frame.minY + translation.y, containerView.frame.width, containerView.frame.height - translation.y)
However, I am unable to do that with UITableView. I have also tried creating a height constraint and changing that but that is also not working. This is what I have tried):
var constH:NSLayoutConstraint?
//in viewDidLoad
constH = NSLayoutConstraint(item: tableView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: tableView.frame.height)
tableView.addConstraint(constH!)
//in drag function
constH = NSLayoutConstraint(item: tableView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: tableView.frame.height - translation.y)
I know the code is right for adding a constraint because I can put a constant there and it works (e.g. 50 changes it to that height).
The entire drag function is below:
func wasDragged(gesture: UIPanGestureRecognizer) {
var yFromCenter: CGFloat = 0
let translation = gesture.translationInView(self.view)
var drag = gesture.view!
var scale = min(200 / abs(yFromCenter), 1)
yFromCenter += translation.y
var yLimitTest = containerView.frame.minY + translation.y
if yLimitTest < minimumY {
drag.center = CGPoint(x: drag.center.x, y: drag.center.y + translation.y)
gesture.setTranslation(CGPointZero, inView: self.view)
drag.transform = CGAffineTransformScale(self.view.transform, scale, scale)
//this IS working
containerView.frame = CGRectMake(containerView.frame.minX, containerView.frame.minY + translation.y, containerView.frame.width, containerView.frame.height - translation.y)
//this doesn't work
constH = NSLayoutConstraint(item: tableView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: tableView.frame.height - translation.y)
//this also doesn't work (but does for UIView containing the UITableView
self.tableView.frame = CGRectMake(tableView.frame.minX, tableView.frame.minY + translation.y, tableView.frame.width, tableView.frame.height - translation.y)
}
}