I have been trying to get this pull to refresh control centered for a while now and I can't seem to get the constraint to work. Am I completely missing something here? I read some tutorials and I read in another question that typically you add position constraints to the parent object. When I tried adding the constraint to the refresh control it told me that it is unable to satisfy constraints.
// Set up the refresh control and add it to the tableView
self.refreshControl.attributedTitle = NSAttributedString(string: "Pull to refresh")
self.refreshControl.addTarget(self, action: "refresh:", forControlEvents: UIControlEvents.ValueChanged)
self.tableView.addSubview(refreshControl)
// Center the refreshControl
var centerControl: NSLayoutConstraint = NSLayoutConstraint(
item: refreshControl,
attribute: NSLayoutAttribute.CenterX,
relatedBy: NSLayoutRelation.Equal,
toItem: tableView,
attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 1.0)
tableView.addConstraint(centerControl)
EDIT: I attempted to set the size of the refreshControl as was suggested and it still appears off to the right like it is 600 wide like the Any Height Any Width view controller. So apparently trying to set the size has changed nothing
Updated code:
override func viewDidLoad() {
super.viewDidLoad()
// Set tableView delegate and dataSource
tableView.delegate = self
tableView.dataSource = self
// Set up the refresh control and add it to the tableView
self.refreshControl = UIRefreshControl(frame: CGRect(x: 0, y: 0, width: view.bounds.width, height: 40))
self.refreshControl.attributedTitle = NSAttributedString(string: "Pull to refresh")
self.refreshControl.addTarget(self, action: "refresh:", forControlEvents: UIControlEvents.ValueChanged)
self.tableView.addSubview(refreshControl)
// Center the refreshControl
let centerControl: NSLayoutConstraint = NSLayoutConstraint(
item: refreshControl,
attribute: NSLayoutAttribute.CenterX,
relatedBy: NSLayoutRelation.Equal,
toItem: refreshControl.superview,
attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0)
refreshControl.superview?.addConstraint(centerControl)
}
Code Update #2: I have tried this and it has not worked either. Am I supposed to have a tableViewController to manage the refreshControl? When I add any of these constraints (even individually) the refresh control gets dragged down with the table. When I don't add constraints it stays put. All I want is to get it so image and text aren't all the way off to the right! Otherwise it works.
override func viewDidLoad() {
super.viewDidLoad()
// Set tableView delegate and dataSource
tableView.delegate = self
tableView.dataSource = self
// Set up the refresh control and add it to the tableView
refreshControl = UIRefreshControl()
refreshControl.attributedTitle = NSAttributedString(string: "Pull to refresh")
refreshControl.addTarget(self, action: "refresh:", forControlEvents: UIControlEvents.ValueChanged)
tableView.addSubview(refreshControl)
refreshControl.setTranslatesAutoresizingMaskIntoConstraints(false)
// Create the constraints and add them
var constX: NSLayoutConstraint =
NSLayoutConstraint(item: refreshControl,
attribute: NSLayoutAttribute.CenterX,
relatedBy: NSLayoutRelation.Equal,
toItem: tableView,
attribute: NSLayoutAttribute.CenterX,
multiplier: 1.0, constant: 0)
tableView.addConstraint(constX)
var constW: NSLayoutConstraint =
NSLayoutConstraint(item: refreshControl,
attribute: NSLayoutAttribute.Width,
relatedBy: NSLayoutRelation.Equal,
toItem: tableView,
attribute: NSLayoutAttribute.Width,
multiplier: 1.0, constant: 0)
tableView.addConstraint(constW)
var constH: NSLayoutConstraint =
NSLayoutConstraint(item: refreshControl,
attribute: NSLayoutAttribute.Height,
relatedBy: NSLayoutRelation.Equal,
toItem: nil,
attribute: NSLayoutAttribute.NotAnAttribute,
multiplier: 1.0, constant: 50)
tableView.addConstraint(constH)
}