Search code examples
iosswiftuiimageconstraintsuilabel

How to centeralize a label after hiding an image next to it


I have a custom cell that has an image and a label in it next to each other. I am reusing the custom cell in another place but I have a condition that hides the image.

How do I set the label to be centered after hiding the image?

I tried to set the text alignment to center but it didn't work.

 if (condition) == false{
            self.checkImage.isHidden = true
        }
        else {
            self.checkImage.isHidden = false
        }

Solution

  • An easy option is to put both the imageView and the label inside a horizontal stackView which will make the label centered when you set isHidden to the imageView

    Or

    Make an outlet of the imageView width and set it's constant to zero