對於任何人看到這個問題在未來,這裏就是我做來解決這個問題:
- (IBAction)showMoreSymptoms:(id)sender {
if(!moreSymptomsExpanded) {
moreSymptomsExpanded = true;
[_moreSymptomsCollectionView setHidden:false];
[_moreSymptomsButton setTitle:@"Less Symptoms" forState:UIControlStateNormal];
//change frames
[_moreSymptomsView setFrame:CGRectMake(_moreSymptomsView.frame.origin.x, _moreSymptomsView.frame.origin.y+_moreSymptomsCollectionView.frame.size.height, _moreSymptomsView.frame.size.width, _moreSymptomsView.frame.size.height)];
//change constraint to the bottom of the new collection view
_higherPriorityMoreSymptomsViewConstraint = [NSLayoutConstraint constraintWithItem:_moreSymptomsView
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:_moreSymptomsCollectionView
attribute:NSLayoutAttributeBottom
multiplier:1
constant:1];
_higherPriorityMoreSymptomsViewConstraint.priority = 1000;
[_moreSymptomsView.superview addConstraint:_higherPriorityMoreSymptomsViewConstraint];
[UIView animateWithDuration:0 animations:^{
[self.view layoutIfNeeded];
}];
} else {
moreSymptomsExpanded = false;
[_moreSymptomsCollectionView setHidden:true];
[_moreSymptomsButton setTitle:@"More Symptoms" forState:UIControlStateNormal];
//reset frames back
[_moreSymptomsView setFrame:CGRectMake(_moreSymptomsView.frame.origin.x, _moreSymptomsView.frame.origin.y-_moreSymptomsCollectionView.frame.size.height, _moreSymptomsView.frame.size.width, _moreSymptomsView.frame.size.height)];
//reset back to original constraint
[_moreSymptomsView.superview removeConstraint:_higherPriorityMoreSymptomsViewConstraint];
[UIView animateWithDuration:0 animations:^{
[self.view layoutIfNeeded];
}];
}
}
- 我把原來的頂部約束優先於故事板,以較低的優先級(750)。
- 當
moreSymptomsButton
被輕敲,我移位moreSymptomsView
的框架向下按預期
- 我創建了一個新的約束頂部稱爲
higherPriorityMoreSymptomsViewConstraint
和toItem
屬性設置爲我展示新的集合視圖(moreSymptomsCollectionView
)與更高的優先級
- 要折回原始狀態,請重置框架並刪除之前創建的約束。
詳細的說明,檢查了這link出
這絕對是一個堅實的方法,它類似於其他答案我見過涉及這個問題。我使用了一個類似的概念,並使其工作,謝謝。 –
@ArjunNayak如果它幫助你,請接受它:) –