如果你正在尋找這個可擴展的,你可能需要在代碼中做到這一點。我使用類似
UIView *superView = /* whatever the superview of your views is, probably self.view in a lot of cases */
NSArray *arrViews = [NSArray arrayWithObjects:/* put only the things you want to show in here, do not put the hidden things, and put them in order from top to bottom */, nil];
CGFloat buffer = 5;
[superView addConstraint:[NSLayoutConstraint constraintWithItem:[arrViews objectAtIndex:0] attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:superView attribute:NSLayoutAttributeTop multiplier:1 constant:buffer]];
for (int i = 1; i < arrViews.count; i++)
{
[superView addConstraint:[NSLayoutConstraint constraintWithItem:[arrViews objectAtIndex:i] attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:[arrViews objectAtIndex:i-1] attribute:NSLayoutAttributeBottom multiplier:1 constant:buffer]];
}
[superView addConstraint:[NSLayoutConstraint constraintWithItem:[arrViews lastObject] attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:superView attribute:NSLayoutAttributeBottom multiplier:1 constant:-1*(buffer)]];
這將會把一個固定的間隔(尺寸= buffer
)頂部的項目之間,它的上海華,那麼每個子視圖和子視圖之間直接上面,然後底部視圖和上海華之間。每次從arrViews
中刪除一個項目,然後致電[superView needsLayout]
,您都必須打電話給您。您還需要確保以某種方式設置高度限制,否則您會收到錯誤。如果一切都將是相同的高度,你可以在循環中添加另一條線以添加高度約束。
+1爲清晰整齊的格式化問題 – Eugene
您是刪除視圖B還是隱藏它?你需要在某個時候恢復它嗎? –