I have designed my custom Cell in IB, subclassed it and connected my outlets to my custom class. I have three subviews in cell content which are: UIView (cdView) and two labels (titleLabel and emailLabel). Depending on data available for each row, sometimes I want to have UIView and two labels displayed in my cell and sometimes only two labels. What I am trying to do is to set constraints that way if I set UIView property to hidden or I will remove it from superview the two labels will move to the left. I tried to set UIView leading constraint to Superview (Cell content) for 10px and UILabels leading Constraints for 10 px to the next view (UIView). Later in my code

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(IndexPath *)indexPath {
    
    // ...

    Record *record = [self.records objectAtIndex:indexPath.row];
    
    if ([record.imageURL is equalToString:@""]) {
         cell.cdView.hidden = YES;
    }
}

我把手机藏起来了。cdView和我想要标签移动到左边,但他们在细胞中保持相同的位置。我试着移除细胞。cdView从superview,但它也没有工作。我附上了图片,以澄清我是什么。

我知道如何通过编程来做到这一点,我不是在寻找解决方案。我想要的是在IB中设置约束,我希望如果其他视图被删除或隐藏,我的子视图将动态移动。有可能在IB自动布局中做到这一点吗?

.....

当前回答

当隐藏属性在它的任何子视图上被改变时,UIStackView自动重新定位它的视图(iOS 9+)。

UIView.animateWithDuration(1.0) { () -> Void in
   self.mySubview.hidden = !self.mySubview.hidden
}

在这段WWDC视频中跳转到11:48进行演示:

自动布局的奥秘,第1部分

其他回答

这是另一个使用优先级约束的解决方案。其思想是将宽度设置为0。

创建容器视图(橙色)并设置宽度。 创建内容视图(红色),并设置尾随空间10pt为superview(橙色)。注意尾随空间约束,有两个优先级不同的尾随约束。低(=10)和高(<=10)。这对于避免歧义很重要。 设置橙色视图的宽度为0以隐藏视图。

这是一个老问题,但我仍然希望它能有所帮助。来自Android,在这个平台上,你有一个方便的方法isVisible隐藏它从视图,但也没有考虑框架时,自动布局绘制视图。

使用extension和“extend”uiview,你可以在ios中做类似的功能(不确定为什么它不在UIKit中)这里是swift 3中的实现:

    func isVisible(_ isVisible: Bool) {
        self.isHidden = !isVisible
        self.translatesAutoresizingMaskIntoConstraints = isVisible
        if isVisible { //if visible we remove the hight constraint 
            if let constraint = (self.constraints.filter{$0.firstAttribute == .height}.first){
                self.removeConstraint(constraint)
            }
        } else { //if not visible we add a constraint to force the view to have a hight set to 0
            let height = NSLayoutConstraint(item: self, attribute: .height, relatedBy: .equal , toItem: nil, attribute: .notAnAttribute, multiplier: 0, constant: 0)
            self.addConstraint(height)
        }
        self.layoutIfNeeded()
    }

对于这个特定的布局,要处理的约束是被隐藏的视图上的'leading'约束。下面的理论在各个方面都适用。

1:设置所有的约束,当所有的视图都是可见的时候,你想让它看起来怎么样。

2:给你想要隐藏的视图添加第二个'leading'约束。这将暂时打破限制。

3:将原来的前导约束的优先级更改为'999' -这将优先级给你的新约束,它将在1000,没有约束将被打破。

4:将新的约束从'leading=leading'更改为' trails =leading'。这将移动您想要隐藏的视图,使其远离父视图的前缘。

5:切换新约束的isActive值现在会切换,如果它在视图中或在视图外。在设置可见性为true/false的同时,将其设置为true/false。例如:

@IBOutlet var avatar:UIImage!
@IBOutlet var avatarLeadHid:NSLayoutConstraint!

func hideAvatar() {
  self.avatar.isHidden = true
  self.avatarLeadHid.isActive = true
}

func showAvatar() {
  self.avatar.isHidden = false
  self.avatarLeadHid.isActive = false
}

额外的好处:你可以调整新的hide -constraint的“常量”值,以改变视图隐藏时使用的填充/边距。这个值可以是负数。

额外的好处:只需切换hide -constraint上的“Installed”复选框,就可以在不运行任何代码的情况下,在Interface Builder中看到你的布局。

进一步帮助:我做了一个视频,展示了我做得更好的点列表:https://youtu.be/3tGEwqtQ-iU

对于谷歌人来说:基于Max的回答,为了解决许多人已经注意到的填充问题,我只是增加了标签的高度,并使用这个高度作为分隔符而不是实际的填充。这个想法可以扩展到任何包含视图的场景。

这里有一个简单的例子:

在本例中,我将Author标签的高度映射到适当的IBOutlet:

@property (retain, nonatomic) IBOutlet NSLayoutConstraint* authorLabelHeight;

当我将约束的高度设置为0.0f时,我们保留了“填充”,因为播放按钮的高度允许它。

我认为这是最简单的答案。请验证它是否有效:

        StackFullView.layer.isHidden = true
        Task_TopSpaceSections.constant = 0.   //your constraint of top view

点击这里查看https://www.youtube.com/watch?v=EBulMWMoFuw