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自动布局中做到这一点吗?

.....

当前回答

当你想隐藏UIView时,创建width约束并将其更改为0,而不是隐藏view。

这可能是最简单的方法。此外,它将保存视图,如果您想再次显示它,则不需要重新创建它(在表单元格中使用非常理想)。要更改常量值,您需要创建一个常量引用出口(与为视图创建出口的方式相同)。

其他回答

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

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

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

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

正如no_scene建议的那样,您可以通过在运行时改变约束的优先级来实现这一点。这对我来说容易得多,因为我有不止一个需要移除的阻塞视图。

下面是使用ReactiveCocoa的代码片段:

RACSignal* isViewOneHiddenSignal = RACObserve(self.viewModel, isViewOneHidden);
RACSignal* isViewTwoHiddenSignal = RACObserve(self.viewModel, isViewTwoHidden);
RACSignal* isViewThreeHiddenSignal = RACObserve(self.viewModel, isViewThreeHidden);
RAC(self.viewOne, hidden) = isViewOneHiddenSignal;
RAC(self.viewTwo, hidden) = isViewTwoHiddenSignal;
RAC(self.viewThree, hidden) = isViewThreeHiddenSignal;

RAC(self.viewFourBottomConstraint, priority) = [[[[RACSignal
    combineLatest:@[isViewOneHiddenSignal,
                    isViewTwoHiddenSignal,
                    isViewThreeHiddenSignal]]
    and]
    distinctUntilChanged]
    map:^id(NSNumber* allAreHidden) {
        return [allAreHidden boolValue] ? @(780) : @(UILayoutPriorityDefaultHigh);
    }];

RACSignal* updateFramesSignal = [RACObserve(self.viewFourBottomConstraint, priority) distinctUntilChanged];
[updateFramesSignal
    subscribeNext:^(id x) {
        @strongify(self);
        [self.view setNeedsUpdateConstraints];
        [UIView animateWithDuration:0.3 animations:^{
            [self.view layoutIfNeeded];
        }];
    }];

使用两个UIStackView水平和垂直,当堆栈中的某些子视图视图被隐藏时,其他堆栈子视图将被移动,使用Distribution ->按比例填充垂直堆栈中的两个uilabel,并需要设置宽度和高度约束的第一个UIView

以下是我如何重新对齐我的uiviews来得到你的解决方案:

拖拽一个UIImageView放到左边。 拖拽一个UIView并把它放到UIImageView的右边。 在UIView中拖放两个uilabel,它的前导约束和尾随约束为零。 将包含2个标签的UIView的前置约束设置为superview而不是UIImagView。 如果UIImageView被隐藏,设置前置约束常量为10像素到superview。ELSE,设置前置约束常量为10px + UIImageView。宽度+ 10像素。

我自己制定了一个拇指规则。当你必须隐藏/显示任何约束可能受到影响的uiview时,在uiview中添加所有受影响/依赖的子视图,并以编程方式更新其前/后/上/下约束常量。

为了帮助别人,我构建了一个使用可视格式约束的helper类。我在我当前的应用程序中使用它。

AutolayoutHelper

它可能有点适合我的需要,但您可能会发现它很有用,或者您可能想修改它并创建自己的帮助器。

我必须感谢Tim的回答,这个关于UIScrollView和本教程的回答。