• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

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

[复制链接]
菜鸟教程小白 发表于 2022-12-13 03:15:44 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我有一个简单的布局,其中有一个从整个屏幕开始的 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));

        }];
    });

}

我在这里做错了什么?



Best Answer-推荐答案


我很惊讶您正在为 View 设置 frame。通常在自动布局中,您可以通过更改约束 constant 而不是通过更改 frame 在 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 中的幻灯片与其父 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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap