I have some auto layout code that is working in iOS 7, but not in iOS 8. My code has not changed between the two versions.
The situation is that I have a view controller that contains a few subviews that should be stacked vertically like so:
However, when I run the same code on iOS 8, the following occurs:
The code that sets up my constraints is as follows:
- (void) updateViewConstraints {
[super updateViewConstraints];
NSDictionary *viewsDictionary = @{
@"timeLabel": self.timeLabel,
@"recordingStateImage": self.recordingStateImage,
@"recordButton": self.recordButton,
@"spacer1": spacer1,
@"spacer2": spacer2,
@"superview": self.view
};
if (!portraitConstraints) {
//
// Portrait Layout
//
portraitConstraints = [[NSMutableArray alloc] initWithArray:
[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[timeLabel][spacer1][recordingStateImage][spacer2(==spacer1)][recordButton(100)]-|"
options:0
metrics:0
views:viewsDictionary]];
[portraitConstraints addObjectsFromArray:@[
[NSLayoutConstraint constraintWithItem:self.timeLabel
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeCenterX
multiplier:1.f
constant:0.f],
[NSLayoutConstraint constraintWithItem:self.recordingStateImage
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeCenterX
multiplier:1.f
constant:0.f],
[NSLayoutConstraint constraintWithItem:self.recordButton
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeCenterX
multiplier:1.f
constant:0.f]
]];
}
[self.view removeConstraints:self.view.constraints];
[self.view addConstraints:portraitConstraints];
}
I am not sure why this is happening. I have examined the workaround noted in the following SO questions:
- Autolayout problems with iOS8 with code that works fine on iOS7
- Issue with Auto Layout on iOS 8 (code works perfectly on iOS 7)
but they have not worked (which is calling setNeedsLayout on the subviews instead of the view controller's view).
If anyone has hit issues w/ NSLayoutAttributeCenterX and auto layout on iOS 8, it would be much appreciated.