ios - NSLayoutConstraint subview 不会自动调整大小以适应容器 View

标签 ios uiviewcontroller autolayout nslayoutconstraint

我有一个简单的布局,其中有一个在整个屏幕上启动的 containerView 。在里面,我有两个垂直 subview - topView(绿色)、botView(橙色),它们需要相等的高度,并且应该具有 containerView 高度的 50%。

启动后,我从屏幕外从底部向上滑动 View (蓝色),导致 containerView 调整大小至较短的高度。

我期望发生的是 topViewbotView 都会变短,在 containerView 内保持均匀的高度,但是实际发生的情况是内部 View 没有调整大小,尽管有这样做的限制。

enter image description here

因为此代码有一些基于代码的约束,以及一些 IB 约束,here是一个示例项目,可以看到它的实际效果。这是被忽略的约束:

NSLayoutConstraint *c1 = [NSLayoutConstraint constraintWithItem:self.topView
                                                      attribute:NSLayoutAttributeHeight
                                                      relatedBy:NSLayoutRelationEqual
                                                         toItem:self.containerView
                                                      attribute:NSLayoutAttributeHeight
                                                     multiplier:0.5f
                                                       constant:0.f];
[self.containerView addConstraint:c1];

和调整 View 大小代码:

- (void)resizeViews {

    __weak UAViewController *weakSelf = self;
    dispatch_async(dispatch_get_main_queue(), ^{
        [UIView animateWithDuration:0.25 animations:^{
            weakSelf.slideInView.frame = CGRectMake(CGRectGetMinX(weakSelf.view.bounds),
                                                    CGRectGetMaxY(weakSelf.view.bounds) - CGRectGetHeight(weakSelf.slideInView.frame),
                                                    CGRectGetWidth(weakSelf.view.bounds),
                                                    CGRectGetHeight(weakSelf.slideInView.frame));

            weakSelf.containerView.frame = CGRectMake(CGRectGetMinX(weakSelf.view.bounds),
                                                      CGRectGetMinY(weakSelf.view.bounds),
                                                      CGRectGetWidth(weakSelf.view.bounds),
                                                      CGRectGetMaxY(weakSelf.view.bounds) - CGRectGetHeight(weakSelf.slideInView.frame));

        }];
    });

}

我在这里做错了什么?

最佳答案

我很惊讶您为 View 设置框架。通常在自动布局中,您可以通过更改约束常量来滑动 View ,而不是通过更改框架

例如,假设您对容器 subview 的约束是这样定义的(为了简单起见,因为两个 View 应该占据容器的 50%,最容易通过将两个 subview 定义为相同来完成高度):

[containerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[topView][botView(==topView)]|" options:0 metrics:nil views:views]];
[containerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[topView]|" options:0 metrics:nil views:views]];
[containerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[botView]|" options:0 metrics:nil views:views]];

并且,为了简单起见,定义容器、 View 中的幻灯片与其 super View 之间的关系,如下所示:

[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[containerView][slideInView]|" options:0 metrics:nil views:views]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[containerView]|" options:0 metrics:nil views:views]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[slideInView]|" options:0 metrics:nil views:views]];

// set the slide in view's initial height to zero

NSLayoutConstraint *heightConstraint = [NSLayoutConstraint constraintWithItem:slideInView
                                                                    attribute:NSLayoutAttributeHeight
                                                                    relatedBy:NSLayoutRelationEqual
                                                                       toItem:nil
                                                                    attribute:NSLayoutAttributeNotAnAttribute
                                                                   multiplier:1.0
                                                                     constant:0.0];
[self.view addConstraint:heightConstraint];

然后,当您想要在“ View 中幻灯片”中滑动时,只需相应地动画改变其高度即可:

heightConstraint.constant = 100;
[UIView animateWithDuration:1.0 animations:^{
    [self.view layoutIfNeeded];
}];

因此:

two views

现在,您也可以使用容器高度的 x% 来执行此操作,但使用 VFL 无法轻松做到这一点,但它应该可以正常工作。另外,您也可以通过为其顶部约束设置动画来滑入“ View 中的幻灯片”(但这很麻烦,因为您必须重新计算旋转的顶部约束,这是我不想在这个简单的模型中执行的操作)。

但你明白了。只需将容器 View 的高度或底部约束定义为与 View 中的幻灯片相关,然后通过调整其约束来为 View 中的幻灯片设置动画,如果您正确定义了所有约束,它将优雅地为所有约束设置动画当您在更改 View 约束中的幻灯片后对 layoutIfNeeded 进行动画处理时,可以正确显示四个 View (容器、 View 中的幻灯片以及容器的两个 subview )。

关于ios - NSLayoutConstraint subview 不会自动调整大小以适应容器 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17977094/

相关文章:

ios - 动态 tableViewCell 中 imageView 的长宽比

ios - 定位和缩放启动屏幕图像

swift - 改变 NSLayoutConstraint

ios - 当 HotSpot 通过 USB 开启时,可达性报告 Wifi

ios - 在WebView中打开所有白名单URL

ios - 回合制多人 iOS 游戏(如 Words With Friends)的模板?

ios - 为我的特定 View Controller 使用扩展

iphone - UIButtons 在第一次加载 View Controller 时没有正确对齐

ios - Cloudkit 返回带有特定字符串的记录

ios - 在 ViewController 中创建数组的预期声明错误,无法找出原因