簡體   English   中英

針對不同設備方向以編程方式且沒有尺寸類別的各種自動布局約束?

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

問題與此相似:自動布局和設備方向

但是從來沒有調用過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()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM