我是新來的自動佈局約束。我在主視圖上有2個視圖(topView和paintView),以及主視圖右上角的按鈕。在加載視圖時,topView佔據整個主視圖(不包括按鈕)。點擊按鈕後,我想讓topView佔據主視圖的70%,並使用paintView佔據其餘部分(不包括按鈕)。 我已經爲使用故事板的topView設置了X,Y和頂部約束。 paintView和相應的約束已經以編程方式設置。使用Autolayout單擊按鈕調整UIViews的大小
我現在的代碼是這樣的:
-(void)setupPaintView
{
UIView *pPaintView = [UIView new];
[pPaintView setBackgroundColor:[UIColor yellowColor]];
pPaintView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:pPaintView];
self.paintView = pPaintView;
[self addConstraintsToView];
//[self setTopViewFrame];
}
-(void)addConstraintsToView
{
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.paintView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.topView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0.0]];
[self.view addConstraint:[NSLayoutConstraint
constraintWithItem:self.paintView
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:self.topView
attribute:NSLayoutAttributeWidth
multiplier:1.0
constant:0.0]];
[self.view addConstraint:[NSLayoutConstraint
constraintWithItem:self.topView
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:self.paintView
attribute:NSLayoutAttributeTop
multiplier:1.0
constant:0.0]];
NSLayoutConstraint *pHeightConstraintTopView = [NSLayoutConstraint
constraintWithItem:self.topView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeHeight
multiplier:1.0
constant:0.0];
self.heightconstraintTopView = pHeightConstraintTopView;
[self.view addConstraint:pHeightConstraintTopView];
NSLayoutConstraint *pHeightConstraintPaintView = [NSLayoutConstraint
constraintWithItem:self.paintView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeHeight
multiplier:0.0
constant:0.0];
self.heightconstraintPaintView = pHeightConstraintPaintView;
[self.view addConstraint:pHeightConstraintPaintView];
}
在按鈕點擊下面的方法被調用:
-(IBAction)detailBtnClick:(id)sender
{
if(self.heightconstraintPaintView.constant == 0)
{
self.heightconstraintTopView.constant = 0.7*self.view.frame.size.height;
self.heightconstraintPaintView.constant = 0.3*self.view.frame.size.height;
[self.view setNeedsUpdateConstraints];
}
else
{
self.heightconstraintTopView.constant = self.view.frame.size.height;
self.heightconstraintPaintView.constant = 0;
[self.view setNeedsUpdateConstraints];
}
}
當視圖加載,冠捷收購主視圖的高度,這是這裏需要。但是當我點擊按鈕時,topView保持在100%,即它不調整大小,paintView也不是。我正在修改topView的常量屬性和paintView約束,但我不確定這是否正確。這裏的約束是必須僅使用Autolay約束來佈置視圖。如何通過點擊按鈕來調整視圖大小? 歡迎任何幫助。
感謝timothykc等人,我成功地解決了上述問題。但是我現在面臨另一個問題。當我將模擬器的方向改變爲橫向時,paintView幾乎保持隱藏狀態。以下是代碼(肘節是決定是否伸展/收縮的觀點的布爾值):
-(IBAction)detailBtnClick:(id)sender
{
if(self.toggle == FALSE)
{
self.topViewHeightConstraint.constant = 0.7*self.bounds.frame.size.height;
self.heightconstraintPaintView.constant = 0.3*self.bounds.frame.size.height;
//[self.view layoutIfNeeded];
}
else
{
self.topViewHeightConstraint.constant = self.view.bounds.size.height;
self.heightconstraintPaintView.constant = 0;
//[self.view layoutIfNeeded];
}
self.toggle = !self.toggle;
}
的topViewHeightConstraint已被添加作爲一個屬性由timothykc所示。這適用於縱向,但對於橫向無法正常工作,因爲topView的高度不會根據需要更改(70%),這意味着比率得不到正確處理。
如果修改常量上的現有約束它的'你需要layoutIfNeeded'打電話,不'setNeedsUpdateConstraints '。 – 2014-09-24 15:42:48