以编程方式且没有大小 类 的不同设备方向的各种自动布局约束?

Various autolayout constraints for different device orientations programmatically and without size classes?

问题与此类似: Autolayout and Device Orientation

但是 updateViewConstraints 从未被调用(因为提到的方法中没有任何内容在旋转时被调用,在 iOS 9.1 模拟器上测试)。总的来说,这个话题似乎没有正确答案或者答案太旧了。

我不能使用大小 类,因为该项目应该也有 iOS 7 支持,它支持大小 类,但有很大的限制。

看来我需要清除并重新创建每个旋转事件的所有约束:

1) 应使用哪种方法重新创建约束?

2) 如何重新创建它们?也许我错误地删除了它们:

for (UIView *v in self.view.subviews)
{
    v.translatesAutoresizingMaskIntoConstraints = NO;
}
for (UIView *v in self.view.subviews)
{
    [v removeConstraints:v.constraints];
    [self.view removeConstraints:v.constraints];
}
[self.view removeConstraints:self.view.constraints];

或者您能否提出解决此问题的更好方法?

您可以在 viewDidLoad 上以编程方式创建两组不同的约束,并且只激活您想要使用的约束

myPortraitConstraint = NSLayoutConstraint(item: someItem, attribute: .Left, relatedBy: .Equal, toItem: self, attribute: .Left, multiplier: 1.0, constant: 10
myPortraitConstraint.active = true

myLandscapeConstraint = NSLayoutConstraint(item: someItem, attribute: .Left, relatedBy: .Equal, toItem: self, attribute: .Left, multiplier: 1.0, constant: 0
myLandscapeConstraint.active = false

然后在旋转事件中切换您希望使用的约束

myPortraitConstraint.active = false
myLandscapeConstraint.active = true
self.view.layoutIfNeeded()