java自动布局框架_自动布局UIScrollView以及具有动态高度的子视图

这是我如何使用容器视图布局纯自动布局UIScrollView的示例 . 我评论说要更清楚:

容器是标准的UIView,而body是UITextView

- (void)viewDidLoad

{

[super viewDidLoad];

//add scrollview

[self.view addSubview:self.scrollView];

//add container view

[self.scrollView addSubview:self.container];

//body as subview of container (body size is undetermined)

[self.container addSubview:self.body];

NSDictionary *views = @{@"scrollView" : self.scrollView, @"container" : self.container, @"body" : self.body};

NSDictionary *metrics = @{@"margin" : @(100)};

//constrain scrollview to superview, pin all edges

[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[scrollView]|" options:kNilOptions metrics:metrics views:views]];

[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[scrollView]|" options:kNilOptions metrics:metrics views:views]];

//pin all edges of the container view to the scrollview (i've given it a horizonal margin as well for my purposes)

[self.scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[container]|" options:kNilOptions metrics:metrics views:views]];

[self.scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-margin-[container]-margin-|" options:kNilOptions metrics:metrics views:views]];

//the container view must have a defined width OR height, here i am constraining it to the frame size of the scrollview, not its bounds

//the calculation for constant is so that it's the width of the scrollview minus the margin * 2

[self.scrollView addConstraint:[NSLayoutConstraint constraintWithItem:self.container attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.scrollView attribute:NSLayoutAttributeWidth multiplier:1.0f constant:-([metrics[@"margin"] floatValue] * 2)]];

//now as the body grows vertically it will force the container to grow because it's trailing edge is pinned to the container's bottom edge

//it won't grow the width because the container's width is constrained to the scrollview's frame width

[self.container addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[body]|" options:kNilOptions metrics:metrics views:views]];

[self.container addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[body]|" options:kNilOptions metrics:metrics views:views]];

}

在我的例子中,'body'是一个UITextView,但它可能是其他任何东西 . 如果您正好使用UITextView,请注意,为了使其垂直增长,它必须具有在viewDidLayoutSubviews中设置的高度约束 . 所以在viewDidLoad中添加以下约束并保持对它的引用:

self.bodyHeightConstraint = [NSLayoutConstraint constraintWithItem:self.body attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:nil multiplier:1.0f constant:100.0f];

[self.container addConstraint:self.bodyHeightConstraint];

然后在viewDidLayoutSubviews中计算高度并更新约束的常量:

- (void)viewDidLayoutSubviews

{

[super viewDidLayoutSubviews];

[self.bodyHeightConstraint setConstant:[self.body sizeThatFits:CGSizeMake(self.container.width, CGFLOAT_MAX)].height];

[self.view layoutIfNeeded];

}

该需要第二个布局传递来调整UITextView的大小 .

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值