Search code examples
swifttableviewwidthuistackview

How to give dynamic width for childStackview in parentStack in Swift


I using stackview in tableview cell

I have one parentStackview with two childStackviews

for parentStackview i have given constraints like below

 leading = 10, trailing = 10, top = 10, height = 50

for two childStackviews in parentStackview i have given like this in storyboard

enter image description here

now if i hide childOneStack then childTwoStack showing in full width in parentStackview.. but i dont want like that i need childTwoStack should half of the parentStackview when i hide childOneStack

code to hide childOneStack in tableview cell:

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

 let cell = tableView.dequeueReusableCell(withIdentifier: "ViewTableVIewCell", for: indexPath) as! ViewTableVIewCell

let bidData = viewData?.result?.bids?[indexPath.row]

if awardData?.status == "A"{

cell.childOneStack.isHidden = true

cell.childTwoStack.frame = CGRect(x: 0, y: 0, width: 200, height: 40)

}
}

class ViewTableVIewCell: UITableViewCell {
@IBOutlet weak var parentStackview: UIStackView!

@IBOutlet weak var childOneStack: UIStackView!
@IBOutlet weak var childTwoStack: UIStackView!
}

error O/p: this is childTwoStack showing full width in parentStackview, please guide me to solve this

enter image description here


Solution

  • You could embed a stack view, which you plan to hide/unhide, in a UIView - this container view will take half of the parent stack even when child stack is hidden.

    You could not hide you views, but change its alpha to 0 - in this case you need to disable user interaction, since the views will be transparent, but still interactable.