簡體   English   中英

iOS:從情節提要中以編程方式重置約束以調整框架更改

[英]iOS: Reset constraints programmatically from storyboard to adjust frame change

使用情節提要,我已經設置了多個視圖,像這樣的滾動視圖

“更多症狀”按鈕下的所有內容都是一個視圖(其后面的“收藏夾視圖”除外)。 我們稱之為moreSymptomsView 現在,我想做的是點擊“更多症狀”按鈕時,我想使用setFramemoreSymptomsView下移,並通過將hidden屬性設置為false來揭示其背后的集合視圖。

它可以正常運行,但是在嘗試滾動后, moreSymptomsView會回到此處的原始位置(由於其限制,我假設是)。

我應該如何以編程方式將moreSymptomsView的約束重置為新集合視圖的底部?

謝謝!

如果要在使用AutoLayout時更改框架。 您應該更改Constraint constant 如果設置setFrame如果與UI進行交互,它將自動返回到以前的狀態。 對於您的情況,您可以這樣做:

  1. 拖放“ More Symptoms頂部約束:

在此處輸入圖片說明

為其命名。 我將其命名為constraintTopSympotomsLabel

  1. 當您想要更改框架時:

    self.constraintTopSympotomsLabel.constant = ValueYouWant

將其移動到新框架。

您可以用相同的方法更改所有約束,以實現所需的框架。

希望有幫助!

對於以后遇到此問題的任何人,以下是我為解決此問題所做的工作:

- (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];
    }];
}

}

  1. 我在情節提要上將原始最高約束的優先級設置為較低的優先級(750)。
  2. 當點擊moreSymptomsButton ,我按預期向下移動了moreSymptomsView的框架
  3. 我創建了一個新的頂部約束,稱為higherPriorityMoreSymptomsViewConstraint ,並將toItem屬性設置為要顯示的新集合視圖(具有更高優先級的moreSymptomsCollectionView
  4. 要折疊回原始狀態,請重置框架並刪除之前創建的約束。

有關更多說明,請查看此鏈接

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM