-3

I have four buttons in a stackView in a collectionViewCell

enter image description here

I am performing segue to a newVC if the button is tapped, the content i am displaying in the newVC is coming via JSON. If the url is empty or url.characters.count == 0 for the respective button, i want to remove it from the cell and resize the rest of the buttons equally in the cell. How can i achieve that.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Usama bin Attique
  • 337
  • 1
  • 3
  • 16
  • 1
    https://developer.apple.com/documentation/uikit/uistackview/1616235-removearrangedsubview – Venk Oct 30 '17 at 13:34

2 Answers2

1

With UIStackView it is very simple. Just mark the button as hidden and the UIStackView will resize itself.

func buttonTapped(_ button: UIButton) {
    button.isHidden = shouldHideButton()

    performSegue(withIdentifier: "YourSegueIdentifier", sender: self)
}

func shouldHideButton() {
    ...
}

Also as venkat mentioned you can use removeArrangedSubview(_:) instead of just hiding, which will remove the button permanently.

I recommand you to read UIStackView documentation.

mickoush
  • 476
  • 4
  • 4
1

Since you're working in a StackView, the other items inside it will automatically adjust when we hide your UIButton.

In order to hide you UIButton based on your URL's character count, we can do the following:

button.isHidden = url.characters.count == 0

To simplify even further, we can just check for .isEmpty like so:

button.isHidden = url.isEmpty
ZGski
  • 2,398
  • 1
  • 21
  • 34