Just a prewarning - I’m pretty sure this issue doesn’t have anything to do with the omission of self.view.layoutIfNeeded()
in the animation.
I’m expecting the constraint ‘animatingSideConstraint’ to animate from the value set in viewWillAppear:
to the new value set (and animated) in viewDidAppear:
, this allows my search bar to appear to grow to the extent of the navigation bar.
The properties get set, but aren’t animated to.
Here’s my code:
@IBOutlet var animatingSideConstraint: NSLayoutConstraint!
override func viewDidLoad()
{
super.viewDidLoad()
// Search Bar
//
searchBar.delegate = self
if let searchBarContainerBounds = navigationController?.navigationBar.bounds
{
searchBarContainerView.bounds = searchBarContainerBounds
}
}
override func viewWillAppear(animated: Bool)
{
super.viewWillAppear(animated)
// Search bar
//
// Set initial properties
//
animatingSideConstraint.constant = CGRectGetWidth(searchBarContainerView.bounds) * 0.5
// Initialise use
//
searchBar.becomeFirstResponder()
}
override func viewDidAppear(animated: Bool)
{
super.viewDidAppear(animated)
// Search bar
//
UIView.animateWithDuration(1.0, animations: { () -> Void in
self.animatingSideConstraint.constant = 0
self.view.layoutIfNeeded()
})
}
I’ve moved the line self.animatingSideConstraint.constant = 0
to before the animation block too, but unsurprisingly it does nothing.
Any help is appreciated!
UPDATE:
override func viewDidAppear(animated: Bool)
{
super.viewDidAppear(animated)
// Search bar
//
self.animatingSideConstraint.constant = 400
NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: "logData", userInfo: nil, repeats: true)
UIView.animateWithDuration(1.0, animations: { () -> Void in
self.view.layoutIfNeeded()
})
}
func logData()
{
NSLog("Constant: %@", self.animatingSideConstraint.constant)
}
gives output:
2014-12-05 18:03:54.249 MyApp[6483:2353740] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
(
"<NSLayoutConstraint:0x174083cf0 H:[UISearchBar:0x12dd092c0]-(NSSpace(0))-| (Names: '|':UIView:0x170182080 )>",
"<NSLayoutConstraint:0x174083d40 H:|-(400)-[UISearchBar:0x12dd092c0] (Names: '|':UIView:0x170182080 )>",
"<NSAutoresizingMaskLayoutConstraint:0x1700856e0 h=--& v=--& H:[UIView:0x170182080(304)]>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x174083cf0 H:[UISearchBar:0x12dd092c0]-(NSSpace(0))-| (Names: '|':UIView:0x170182080 )>
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
2014-12-05 18:03:54.361 MyApp[6483:2353740] Constant: 400
2014-12-05 18:03:54.455 MyApp[6483:2353740] Constant: 400
2014-12-05 18:03:54.556 MyApp[6483:2353740] Constant: 400
2014-12-05 18:03:54.655 MyApp[6483:2353740] Constant: 400
2014-12-05 18:03:54.755 MyApp[6483:2353740] Constant: 400
2014-12-05 18:03:54.856 MyApp[6483:2353740] Constant: 400
2014-12-05 18:03:54.955 MyApp[6483:2353740] Constant: 400
2014-12-05 18:03:55.056 MyApp[6483:2353740] Constant: 400
2014-12-05 18:03:55.155 MyApp[6483:2353740] Constant: 400
2014-12-05 18:03:55.255 MyApp[6483:2353740] Constant: 400
2014-12-05 18:03:55.355 MyApp[6483:2353740] Constant: 400
2014-12-05 18:03:55.455 MyApp[6483:2353740] Constant: 400
[repeated]
UPDATE 2
Here’s the constraint in a visual format, the constraint I’m animating is the leading constraint.
Starting properties:
|-(half the search bar’s width)-[the search bar]-0-|
End properties:
|-0-[the search bar]-0|
Mid properties halfway through animation, assuming for simplicity the search bar’s width is 100
|-50-[the search bar]-0-|
UPDATE 3:
Note, the leading constraint is the one referenced in the IBOutlet