简体   繁体   English

removeFromSuperview函数无法正常工作

[英]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. 但是当我通过使用removeFromSuperview函数删除img1 ,找不到其他图像removeFromSuperview for example if I removed img1 , other imageviews appear on each other: 例如,如果我删除了img1 ,则其他图像视图彼此出现:

img23456

I don't want to use hide functions, because a space between them will be appear. 我不想使用hide函数,因为它们之间会出现一个空格。 for example if I remove img1 : 例如,如果我删除img1

'  ' img2 img3 img3 img4 img5 img6

on other hand, what I want is if img1 removed (without first space): 另一方面,我想要的是如果img1被删除(没有第一个空格):

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. 所以问题是,如果从superView中删除img1 UIImageView 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 . 您对与已删除的UIImageViews关联的其余UIImageViews约束将失败,因为它们已与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. 硬解决方案:当您通过代码从superView中删除“链”中的某些内容时,需要为每个UIImageView更新/连接/添加约束。

Better solution: (Best practice) 更好的解决方案:(最佳做法)

Remove all your UIImageViews and add a single UICollectionView . 删除所有UIImageViews并添加一个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 . 将一个UIImageView添加到该单元格,然后根据需要简单地deleteadd单元格, UICollectionView将为您处理所有布局

@sneak's answer is the standard way to solve this problem. @sneak的答案是解决此问题的标准方法。

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 . spacing constraints设为紧邻的左UIImageViewmultiplied constant

Then take the IBOutlet of the width constraint of img's . 然后采用img's的width constraintIBOutlet Then change your else part like this -> 然后像这样更改您的其他部分->

else{
       widthImg1.constant = 0
}

Now the spacing will also become zero because it's a multiplied constant. 现在,间距也将变为零,因为它是一个乘常数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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