2017-04-14 21 views
0

​​斯威夫特UIStackView - 動態視圖高地細胞之間的間距較大編程

我與編程UIStackView工作,在給定的位置動態插入一個表格。按下按鈕並將UIView插入0位置。當用戶單擊表單上的保存時,約束被抹去,單元格被移除,然後被替換爲顯示UIView。替換UIView可以根據從表單內記錄的文本數量進行變化。

我遇到的問題是,當第一個單元格有長文本和第二個單元格輸入短文本,兩個單元格不刷新。如果高度相同,則不會發生這種情況。因此,如果第一個單元格的高度與第二個單元格的高度相同,則所有三個單元格都將在頂部顯示爲齊平。

上圖顯示了我的問題以及我希望實現的目標。我該如何解決?

另外,我看到的問候下面的輸出到我的約束:

LayoutConstraints] Unable to simultaneously satisfy constraints. 
Probably at least one of the constraints in the following list is one you don't want. 
Try this: 
    (1) look at each constraint and try to figure out which you don't expect; 
    (2) find the code that added the unwanted constraint or constraints and fix it. 
(
"<NSLayoutConstraint:0x61000028ac80 MyApp.StepView:0x7fdf4b74b360.height == 166 (active)>", 
"<NSLayoutConstraint:0x6100002870d0 MyApp.StepView:0x7fdf4b678da0.height == 166 (active)>", 
"<NSLayoutConstraint:0x61000028db10 'UISV-canvas-connection' UIStackView:0x7fdf4e8012d0.top == MyApp.StepView:0x7fdf4b74b360.top (active)>", 
"<NSLayoutConstraint:0x610000289d80 'UISV-canvas-connection' V:[MyApp.StepView:0x7fdf4b678da0]-(0)-| (active, names: '|':UIStackView:0x7fdf4e8012d0)>", 
"<NSLayoutConstraint:0x61000028a820 'UISV-spacing' V:[MyApp.StepView:0x7fdf4b74b360]-(15)-[MyApp.StepView:0x7fdf4b678da0] (active)>", 
"<NSLayoutConstraint:0x61800009acc0 'UIView-Encapsulated-Layout-Height' UIStackView:0x7fdf4e8012d0.height == 770 (active)>" 
) 

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x6100002870d0 MyApp.StepView:0x7fdf4b678da0.height == 166 (active)> 

這是一個漂亮的定製設計,一點也不像典型的移動佈局。我想知道如果我放棄UIStackView並使用約束條件會更好。我認爲UIStackView,因爲我想能夠增加/刪除元素與調整自己的大小。

回答

0

我發現了一種通過測量堆棧視圖中的所有單元格來獲得所需結果的方法。如果該值小於500,我將一個空白單元格推入堆棧頂部。這確保了所有其他細胞似乎對齊。

由於我基本上每次都重建StackView,所以這種方法最有意義。