[英]iOS: Reset constraints programmatically from storyboard to adjust frame change
對於以后遇到此問題的任何人,以下是我為解決此問題所做的工作:
- (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];
}];
}
}
moreSymptomsButton
,我按預期向下移動了moreSymptomsView
的框架 higherPriorityMoreSymptomsViewConstraint
,並將toItem
屬性設置為要顯示的新集合視圖(具有更高優先級的moreSymptomsCollectionView
) 有關更多說明,請查看此鏈接
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.