简体   繁体   中英

removeFromSuperview function doesn't work fine

I have six imageviews and I set them like this :

img1 img2 img3 img4 img5 img6

but when I remove img1 by using removeFromSuperview function other imageviews doesn't appear find. for example if I removed img1 , other imageviews appear on each other:

img23456

I don't want to use hide functions, because a space between them will be appear. for example if I remove img1 :

'  ' img2 img3 img3 img4 img5 img6

on other hand, what I want is if img1 removed (without first space):

img2 img3 img3 img4 img5 img6

my layout:

在此处输入图片说明

updated

my code:

    if(self.job.wifi == "1")
    {
                    self.img_option_1.isHidden = false
        self.img_option_1.image = UIImage(named: "wififree")

    }else{
        self.img_option_1.removeFromSuperview()
    }

    if(self.job.apple_health == "1")
    {
        self.img_option_2.isHidden = false
        self.img_option_2.image = UIImage(named: "sib_noghrei")
    }else{
       // self.img_option_2.removeFromSuperview()
        self.img_option_2.removeFromSuperview()
    }

    if(self.job.wc == "1")
    {

        self.img_option_3.isHidden = false
        self.img_option_3.image = UIImage(named: "wc")
    }else{
        self.img_option_3.removeFromSuperview()
    }

    if(self.job.full_time == "1")
    {
        self.img_option_4.isHidden = false
        self.img_option_4.image = UIImage(named: "fulltime")
    }else{
        self.img_option_4.removeFromSuperview()
    }


    if(self.job.pos == "1")
    {

        self.img_option_5.isHidden = false
        self.img_option_5.image = UIImage(named: "pos")
    }else{
        self.img_option_5.removeFromSuperview()
    }

    if(self.job.parking == "1")
    {
        self.img_option_6.isHidden = false
        self.img_option_6.image = UIImage(named: "parking")
    }else{
        self.img_option_6.removeFromSuperview()
    }

So the problem is, If you remove img1 UIImageView from your superView. Your constraints on the rest of the UIImageViews that are connected to the one you removed will fail, because they are connected to img1 UIImageView .

Hard solution: You need to update/connect/add your constraints for every UIImageView , when you are removing something in the "chain" from superView by code.

Better solution: (Best practice)

Remove all your UIImageViews and add a single UICollectionView .

Add one UIImageView to the cell, and simply delete or add cells as you wish, and the UICollectionView will handle all the layout for you .

@sneak's answer is the standard way to solve this problem.

I have one more idea to share, which I used in my app.

Make the spacing constraints as the multiplied constant of the immediate left UIImageView .

Then take the IBOutlet of the width constraint of img's . Then change your else part like this ->

else{
       widthImg1.constant = 0
}

Now the spacing will also become zero because it's a multiplied constant.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM