Here is some puzzling issue related to constraints in an iOS app.
The code is:
view.addConstraints([NSLayoutConstraint(item: infoLabel,
attribute: .centerX,
relatedBy: .equal,
toItem: view,
attribute: .centerX,
multiplier: 1.0,
constant: 0.0),
NSLayoutConstraint(item: infoLabel,
attribute: .top,
relatedBy: .equal,
toItem: iconView,
attribute: .bottom,
multiplier: 1.0,
constant: 50.0)])
The code above is executed when in portrait mode. The constraints used for landscape mode being irrelevant for the present question I do not include them. On an iPhone 6 with iOS 10.2.1 it works perfectly. The problem is on an iPhone 4 (3.5 inches) and on an iPod Touch (4 inches), both with iOS 9.3.5; the first constraint (with .centerX) does not work.
More precisely, the problem appears when rotating the device coming back from landscape mode, the centering is no longer performed as expected. In other words, it is working fine when displaying in portrait mode at start. I wonder if there could be a miss in my code or if this is due to a difference of iOS versions or a difference of devices.
Additionnal information: Constraints used in landscape mode:
view.addConstraints([NSLayoutConstraint(item: infoLabel,
attribute: .right,
relatedBy: .equal,
toItem: view,
attribute: .right,
multiplier: 1.0,
constant: -30.0),
NSLayoutConstraint(item: infoLabel,
attribute: .centerY,
relatedBy: .equal,
toItem: view,
attribute: .centerY,
multiplier: 1.0,
constant: 20.0)])
And before those constraints are set, this line of code is executed:
view.removeConstraints(view.constraints)