2015-12-24 21 views
2

使用故事板,我已經設置了通過滾動視圖涵蓋像「更多症狀」下thisiOS裝置:從故事板編程重置約束來調整幀改變

一切多個視圖按鈕是一個視圖(除收集它在後面查看)。我們稱之爲moreSymptomsView。現在我想要做的是在點擊「更多症狀」按鈕時,我想將moreSymptomsView向下移動setFrame,並通過將hidden屬性設置爲false來顯示其背後的集合視圖。

它功能正常,但在嘗試滾動後,moreSymptomsView回到原來的位置,如here(我假設由於它的限制)。

我該如何着手將moreSymptomsView的約束條件重置爲新集合視圖的底部?

謝謝!

回答

2

如果您想在使用AutoLayout時更改框架。您應該更改constantConstraint。如果你setFrame它會自動回到以前的狀態,如果有與UI的互動。它

enter image description here

名稱:隨着你的情況,你可以這樣做:

  1. 拖放的More Symptoms頂部約束。我把它命名constraintTopSympotomsLabel

  2. 當你想改變它的幀:

    self.constraintTopSympotomsLabel.constant = ValueYouWant

它移動到新的框架。

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

希望得到這個幫助!

+0

這絕對是一個堅實的方法,它類似於其他答案我見過涉及這個問題。我使用了一個類似的概念,並使其工作,謝謝。 –

+0

@ArjunNayak如果它幫助你,請接受它:) –

1

對於任何人看到這個問題在未來,這裏就是我做來解決這個問題:

- (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. 我創建了一個新的約束頂部稱爲higherPriorityMoreSymptomsViewConstrainttoItem屬性設置爲我展示新的集合視圖(moreSymptomsCollectionView)與更高的優先級
  4. 要折回原始狀態,請重置框架並刪除之前創建的約束。

詳細的說明,檢查了這link

+0

偉大的,這比我的解決方案更好。謝謝。 –