[英]Find in code a layout constraint created in IB
我想用代碼制作在IB中創建的約束的動畫。 我想找到底部LayoutGuide和UIView底部邊緣之間的約束。
約束實例非常不透明:firstItem和secondItem是AnyObject,因此有很多強制轉換。 除了在_stdlib_getTypeName()上進行字符串比較外,很難看到我將如何決定哪些約束涉及LayoutGuides。
還是應該刪除所有約束並即時重新創建它們? (但是,IB的意義何在?由於我的情節提要板使用自動布局,因此無論如何我都必須在IB中添加約束。)
在Interface Builder中單擊約束,並為其創建IBOutlet
,就像對按鈕,文本視圖等一樣。
您還可以在運行時使用類似以下的內容找到它:
NSLayoutConstraint *desiredConstraint;
for (NSLayoutConstraint *constraint in putYourViewHere.constraints) {
if (constraint.firstAttribute == NSLayoutAttributeHeight) { // Or whatever attribute you're looking for - you can do more tests
desiredConstraint = constraint;
break;
}
}
這應該很簡單。 如前所述,為要設置動畫的約束創建一個IBOutlet:
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *verticalSpaceLayoutConstraint;
然后,當您要對其進行動畫處理時(另請參見: 如何為約束更改設置動畫? ),如果需要在具有約束的視圖中強制進行布局傳遞,將約束更新為所需的值,執行UIView動畫並強制布局傳遞為動畫持續時間所需:
- (void)moveViewSomewhere {
[self.view layoutIfNeeded];
_verticalSpaceLayoutConstraint.constant = 10; // The value you want your constraint to have when the animation completes.
[UIView animateWithDuration:1.0
animations:^{
[self.view layoutIfNeeded];
}];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.