[英]iOS: Set View Size in Code with Autolayout enabled
我在標簽欄導航中有兩個視圖控制器。 在第二個場景中,我有一個附加視圖(只是一個簡單的UIView)和一個用於設置其顏色和范圍的按鈕。
CGRect viewRect = CGRectMake(20, 20, 70, 70);
self.animationView.bounds = viewRect;
self.animationView.backgroundColor =
[UIColor yellowColor];
此代碼可以正常工作。 但是,如果我導航到第一個視圖控制器,然后又回到第二個視圖控制器,我的視圖仍然是黃色的,但它又回到了在界面生成器中設置的大小和位置。
我該如何預防? 如果我禁用自動版式,但我真的不想這樣做,則此行為結束。
為animationView約束創建出口,並更改其恆定值。
在viewcontroller的.h文件中:將出口連接到IB中的正確約束:
//AnimationView Height Constraint
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *cHeight;
//AnimationView Width Constraint
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *cWidth;
//AnimationView Leading Constraint
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *cLeading;
//AnimationView Top Constraint
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *cTop;
在ViewController的.m文件中,設置約束的常量值,而不是框架:
- (IBAction)btnTouched:(id)sender {
[_cHeight setConstant:70];
[_cWidth setConstant:70];
[_cTop setConstant:20];
[_cLeading setConstant:20];
self.animationView.backgroundColor = [UIColor yellowColor];
}
會很好的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.